Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 623
Passionate programmer Avatar asked Jun 11 '10 06:06

Passionate programmer


People also ask

What is the main difference between HashMap and LinkedHashMap?

The key difference between HashMap and LinkedHashMap is order. Elements of a HashMap are not in order, totally random, whereas elements of LinkedHashMap are ordered. The entries of a LinkedHashMap are in key insertion order, which is the order in which the keys are inserted in the Map.

What is the difference between HashMap and LinkedHashMap pick one option?

Generally, unless there is a reason not to, you would use HashMap. That is, if you need to get the keys back in insertion order, then use LinkedHashMap. If you need to get the keys back in their true/natural order, then use TreeMap. Otherwise, HashMap is probably best.

How is a LinkedHashMap implemented?

LinkedHashMap uses doubly Linked List for doing so. By using before and after - we keep track of newly added entry in LinkedHashMap, which helps us in maintaining insertion order. Before refers to previous entry and after refers to next entry in LinkedHashMap.

Is LinkedHashMap better than HashMap?

On the other hand, HashMap doesn't maintain any order or keys, or values. In terms of performance, there is not much difference between HashMap and LinkedHashMap but yes LinkedHashMap has more memory footprint than HashMap to maintain doubly LinkedList which it uses to keep track of the insertion order of keys.


2 Answers

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.

like image 132
Jon Skeet Avatar answered Oct 13 '22 12:10

Jon Skeet


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 and LinkedHashMap 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.

like image 45
Stephen C Avatar answered Oct 13 '22 12:10

Stephen C