Java – Question regarding LinkedList in Java

arraycollectionsiteratorjavalinked list

When I was reading a book for SCJP, I came across the following paragraph.

A LinkedList is ordered by index position, like ArrayList, except that
the elements are doubly-linked to one another. This linkage gives you
new methods (beyond what you get from the List interface) for adding
and removing from the beginning or end, which makes it an easy choice
for implementing a stack or queue. Keep in mind that a LinkedList may
iterate more slowly than an ArrayList, but it's a good choice when you
need fast insertion and deletion..

What makes a LinkedList to iterate more slowly than an ArrayList ?

Best Answer

Here is an answer from stackoverflow that explains it pretty well:

https://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster

Basically, the ArrayList is contiguous where the LinkedList is not. Incrementing to the next location in memory with the ArrayList is considered faster than jumping to the next location via a reference in LinkedList. Also, maintenance of the LinkedList would incur overhead to maintain two sets of references for a doubly linked list.

Related Topic