Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is add at specific index slower in LinkedLists than in ArrayLists

I was checking this chart containing performances comparisons among lists:

enter image description here

And I find it very odd that adding and removing elements at a specific index performs faster in an ArrayList than it does in a LinkedList. Is this chart wrong? The whole idea of a LinkedList is to improve such operations, while paying with reduced iteration performance. But this chart seems to indicate that Java's implementation of LinkedLists is very poorly done.

Should I trust the chart? And if so, why Java LinkedLists perform so bad?

like image 413
FinnTheHuman Avatar asked Jun 07 '16 16:06

FinnTheHuman


People also ask

Which collection class is slower while adding data at random indexes?

ArrayList is faster in accessing random index data, but slower when inserting elements in the middle of the list, because using linked list you just have to change reference values.

Why accessing an element in a LinkedList is slower?

It is just that indexing a LinkedList is inefficient. In general, it takes O(N) steps to perform a get(i) in a linked list of length N . By contrast with an array, or an ArrayList , you can retrieve any element of the data structure in one step.

Are Arraylists or linked lists faster?

ArrayList is faster in storing and accessing data. LinkedList is faster in manipulation of data.

Why insertion and deletion is slower in ArrayList?

Secondly, inserting into ArrayList is generally slower because it has to grow once you hit its boundaries. It will have to create a new bigger array, and copy data from the original one.


2 Answers

The whole idea of a LinkedList is to improve such operations, while paying with reduced iteration performance.

No, the idea of a linked list is to make adding or removing at the end or beginning very cheap... or if you have a reference to a node object already, adding around that or removing it is very cheap too. (I don't think the Java API exposes the nodes, but some other platforms like .NET do.)

To add at an index within a linked list, the implementation first has to navigate to the node at that index... which is an O(n) operation. Once it's got there, the add is cheap. So adding by index near the start (or end with a smart implementation) is cheap, but adding by index near the middle is expensive.

With an ArrayList, the expense comes from:

  • Copying the existing elements beyond the index you're adding
  • Copying the whole thing is the buffer isn't big enough
like image 139
Jon Skeet Avatar answered Sep 22 '22 14:09

Jon Skeet


A general information:

Making micro benchmark is influenced by many factors, so it is not so easy to define a micro benchmark that is reliable for little numbers.


The insertion can be seen as a two step process:

  • Find position
  • Insert element

The first step, find position, is done in O(n), the second step in O(1) for a LinkedList.

Using an ArrayList finding the position is O(1), instead the insertion is done in O(n). Moving records one position (to create space for a new element) is done using native libraries so it is a quite fast operation.

like image 20
Davide Lorenzo MARINO Avatar answered Sep 21 '22 14:09

Davide Lorenzo MARINO