How is the implementation of LinkedHashMap different from HashMap?
If LinkedHashMap's time complexity is same as HashMap's complexity why do we need HashMap? What are all the extra overhead LinkedHashMap has when compared to HashMap in Java?
Solution 1:
LinkedHashMap will take more memory. Each entry in a normal HashMap
just has the key and the value. Each LinkedHashMap
entry has those references and references to the next and previous entries. There's also a little bit more housekeeping to do, although that's usually irrelevant.
Solution 2:
If LinkedHashMap's time complexity is same as HashMap's complexity why do we need HashMap?
You should not confuse complexity with performance. Two algorithms can have the same complexity, yet one can consistently perform better than the other.
Remember that f(N) is O(N)
means that:
limit(f(N), N -> infinity) <= C*N
where C
is a constant. The complexity says nothing about how small or large the C
values are. For two different algorithms, the constant C
will most likely be different.
(And remember that big-O complexity is about the behavior / performance as N
gets very large. It tells you nothing about the behavior / performance for smaller N
values.)
Having said that:
The difference in performance between
HashMap
andLinkedHashMap
operations in equivalent use-cases is relatively small.A
LinkedHashMap
uses more memory. For example, the Java 11 implementation has two additional reference fields in each map entry to represent the before/after list. On a 64 bit platform without compressed OOPs the extra overhead is 16 bytes per entry.Relatively small differences in performance and/or memory usage can actually matter a lot to people with performance or memory critical applications1.
1 - ... and also to people who obsess about these things unnecessarily.
Solution 3:
- LinkedHashMap additionally maintains a doubly-linked list running through all of its entries, that will provide a reproducable order. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
- HashMap doesn't have these extra costs (runtime,space) and should prefered over LinkedHashMap when you don't care about insertion order.
Solution 4:
LinkedHashMap is a useful data structure when you need to know the insertion order of keys to the Map. One suitable use case is for the implementation of an LRU cache. Due to order maintenance of the LinkedHashMap, the data structure needs additional memory compared to HashMap. In case insertion order is not a requirement, you should always go for the HashMap.
Solution 5:
There is another major difference between HashMap and LinkedHashMap :Iteration is more efficient in case of LinkedHashMap.
As Elements in LinkedHashMap are connected with each other so iteration requires time proportional to the size of the map, regardless of its capacity. But in case of HashMap; as there is no fixed order, so iteration over it requires time proportional to its capacity.
I have put more details on my blog.