aspose file tools*
The moose likes Java in General and the fly likes index access Vs iterator access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "index access Vs iterator access" Watch "index access Vs iterator access" New topic
Author

index access Vs iterator access

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

hi ranchers ,

index access Vs iterator access : which one is fast ?

as in my view index access is fast since it provide direct access to collection elements As for iterator provides sequential access .

share your opinion.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

That depends on the data structure.

Arrays and ArrayList provide efficient random access, which means that you can quickly lookup an element by index. But LinkedList does not provide efficient random access; to find the element at a specific index in a LinkedList, the program must walk over all elements in the list until the required element.

If you're just looping through the elements of an array or ArrayList, then it does not really matter if you do it by index or via an iterator; both will be approximately equally fast.

If you're looping through the elements of a LinkedList, then doing it via an index will be slower than using an iterator, because for every element the program will have to walk trough the list to find the element at the specified index.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Jesper de Jong wrote:If you're just looping through the elements of an array or ArrayList, then it does not really matter if you do it by index or via an iterator; both will be approximately equally fast.
If you're looping through the elements of a LinkedList, then doing it via an index will be slower than using an iterator...

All of which means that if you're looping through the elements of a List (ie, you don't know what the implementation is), you should generally use an Iterator.

And just to add to what Jesper said: Lists that have good random-access performance are supposed to implement java.util.RandomAccess, which you can check for with instanceof.
All the Lists in the standard libraries follow this rule; but there may be 3rd-party ones that don't.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

On a related note, prefer the enhanced for loop to the regular one.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

Stephan is right, and the enhanced for loop syntax will make use of an Iterator below the covers, so that it's efficient for both ArrayList and LinkedList.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: index access Vs iterator access