aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes LinkedList vs ArrayList Iteration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "LinkedList vs ArrayList Iteration" Watch "LinkedList vs ArrayList Iteration" New topic
Author

LinkedList vs ArrayList Iteration

Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Hi all,

Can someone explain me Why LinkedList is faster than ArrayList from iterating point of view ?

Is it because the underlying implementation of LinkedList is doubly link list ?

TIA


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Any takers ?
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
I will assume that your premise is true that "linkedList is faster than ArrayList from iterating point of view."

A look at the class' hierarchies and their interfaces reveals the following:


Judging simply by the collections' superclasses' names and their interfaces, I'd think that LinkedList would be faster to iterate over the entire collection because LinkedList has a superclass called AbstractSequentialList, which may be aptly named because of its underlying implementation.

ArrayList implements an interface named RandomAccess, which again seems to hint at some underlying functinality. The RandomAccess interface indicates to me that seeking any single member via random access should be faster than through iterating over the entire collection via sequential access.

Your question is "Why (is) LinkedList faster than ArrayList from iterating point of view?" My answer, based solely on the interface and superclass names is that iterating over an entire collection is faster (or, at least by my intuition, should not be slower) via sequential than random access.
Mathangi Shankar
Ranch Hand

Joined: Nov 01, 2004
Posts: 56
For iteration or faster access ArrayList adds better performance.
For faster insertion LinkedList is used.

This is because ArrayList is not synchronized and hence the speed for accessing the elements using the index from the list is faster.

LinkedList implements doublyLinkedList so access to elements requires the list to be traversed using the links.

Mathangi.


--------------------------------<br />SCJP1.4
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Thanks J Borderi that helps.

Mathangi,
I agree with you on the fact that ArrayList is not synchronized. However, LinkList is not synchronized either. It is only the Vector which is synchronized.
One more thing to add, LinkList IS FASTER than ArrayList for iterating. Please follow this link here for more details.

Thanks to both of you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LinkedList vs ArrayList Iteration