Why HashMap internally use s LinkedList instead of Arraylist when two objects are placed in the same basket in a hash table?
In fact, he does not use either (!).
In fact, it uses a singly linked list implemented by a chain of hash table entries. (In contrast, a LinkedList is linked twice, and it requires a separate Node object for each item in the list.)
So why am I picking on here? Because it is really important ... because it means that the usual compromise between LinkedList and ArrayList not applicable.
The usual compromise:
ArrayList uses less space, but inserting and deleting the selected item in the worst case is O(N) .
LinkedList uses more space, but inserting and deleting the selected item is O(1) .
However, in the case of a private, single-linked list, formed by combining together the HashMap input nodes, the service memory of the space is one link (the same as ArrayList ), the cost of inserting the node is O(1) (the same as [TG414). ]), and the cost of deleting the selected node is also O(1) (similar to LinkedList ).
Relying only on βbig Oβ for this analysis is doubtful, but if you look at the real code, it becomes clear that the HashMap outperforms the ArrayList in performance for deletion and insertion, and is comparable in search. (This ignores the effects of locality of memory.) In addition, it uses less memory for chaining than ArrayList or LinkedList ... given that there are already internal input objects for storing key / value pairs.
But it gets even harder. In Java 8, they redesigned the internal HashMap data structures. In the current implementation, when the hash chain exceeds a certain length threshold, the implementation switches to using a binary tree view if the key type implements Comparable .
source share