How is the implementation of LinkedHashMap different from HashMap?
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.
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.
- 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.
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.