I am trying to to understand why Java's ArrayDeque is better than Java's LinkedList as they both implement Deque interface.
I hardly see someone using ArrayDeque in their code. If someone sheds more light into how ArrayDeque is implemented, it would be helpful.
If I understand it, I will be more confident using it. I could not clearly understand the JDK implementation as to the way it manages head and tail references.
Linked structures are possibly the worst structure to iterate with a cache miss on each element. On top of it they consume way more memory.
If you need add/remove of the both ends, ArrayDeque is significantly better than a linked list. Random access each element is also O(1) for a cyclic queue.
The only better operation of a linked list is removing the current element during iteration.
All the people criticizing a
LinkedList
, think about every other guy that has been usingList
in Java probably usesArrayList
and anLinkedList
most of the times because they have been before Java 6 and because those are the ones being taught as a start in most books.But, that doesn't mean, I would blindly take
LinkedList
's orArrayDeque
's side. If you want to know, take a look at the below benchmark done by Brian.The test setup considers:
Test Result:
Graph:
Takeaway:
ArrayList
orArrayDeque
with a good guess of maximum capacity that the list may be required to be because of strict memory constraint.LinkedList
so tread carefully when deciding to use aArrayDeque
especially because it DOESN'T implement theList
interface(I think that's reason big enough). It may be that your codebase talks to the List interface extensively, most probably and you decide to jump in with anArrayDeque
. Using it for internal implementations might be a good idea...ArrayDeque
is new with Java 6, which is why a lot of code (especially projects that try to be compatible with earlier Java versions) don't use it.It's "better" in some cases because you're not allocating a node for each item to insert; instead all elements are stored in a giant array, which is resized if it gets full.
although
ArrayDeque<E>
andLinkedList<E>
have both implementedDeque<E>
Interface, but the ArrayDeque uses basically Object arrayE[]
for keeping the elements inside its Object, so it generally uses index for locating the head and tail elements.In a word, it just works like Deque (with all Deque's method), however uses array's data structure. As regards which one is better, depends on how and where you use them.
I believe that the main performance bottleneck in
LinkedList
is the fact that whenever you push to any end of the deque, behind the scene the implementation allocates a new linked list node, which essentially involves JVM/OS, and that's expensive. Also, whenever you pop from any end, the internal nodes ofLinkedList
become eligible for garbage collection and that's more work behind the scene. Also, since the linked list nodes are allocated here and there, usage of CPU cache won't provide much benefit.If it might be of interest, I have a proof that adding an element to
ArrayList
orArrayDeque
runs in amortized constant time; refer to this.Accessing an element in ArrayDeque is always faster compared to LinkedList with O(1) for accessing elements. In Linked list, it will take O(N) to find last element.
ArrayDeque is memory efficient since you don't have to keep track of next node unlike in Linked List.
Even as per java documentation, it has been quoted that
Benchmarking of these two proves that ArrayDeque is faster.