Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

Difference between HashMap vs TreeMap vs LinkedHashMap in Java

Though all three classes like HashMap, LinkedHashMap and TreeMap are implementation of java.util.Map interface, there is some functional difference between them. Perhaps most notable difference between them comes from their iteration order. HashMap makes absolute no guarantee about in which order you can iterate their keys, any application depending upon iteration order of HashMap is fragile, because it can change anytime. Infact, in Java 7, iteration order of HashMap is different than Java 6. On the other hand TreeMap is a SoretedMap and keeps their key in particular sorted order, enforced by either natural order of keys or Comparator instance provided during construction of TreeMap. 

The third Map, LinkedHashMap can maintain its keys in two order, either insertion order (on which those keys are inserted) or access order (on which they are accessed). By default, LinkedHashMap keeps keys in insertion order. If you want to maintain access order, just provide true to access order boolean parameter. 

Another crucial difference between HashMap, TreeMap and LinkedHashMap comes from their performance for common operations like get(), put(), remove() and containsKey(); both HashMan and LinkedHashMap provides O(1) performance, whiel TreeMap provides O(log(n)), so it's slightly slow compared to other two. 


This is actually the cost you need to pay to keep keys in their sorting order. Apart fromt these major differnces, we will learn couple of more in next section.


Difference between HashMap, LinkedHashMap and TreeMap

Here are some important difference between TreeMap, LinkedHashMap, and HashMap in Java on point format

1. Order
HashMap doesn't maintain any order, TreeMap keeps all elments in sorted order, specified by Comparator or object's natural order defined by Comparable. LinkedHashMap keeps elements in the same order they are inserted into map. 

2. Performance
HashMap gives best performance because there is no overhead, TreeMap gives slower performance because every time you add or remove mapping , it need to sort the whole map. LinkedHashMap gives performance in between, 


3. Null keys and values
HashMap doesn't all null keys but allows null value, but TreeMap doesn't allow null key. LinkedHashMap allows null key. 


4. Fail-fast behavior
Iterator of all map are fail-fast in nature. 


5. Internal implementation
HashMap is internally based upon hash table data structure, TreeMap is based upon Red Black Tree and LinkedHashMap uses doubly linked list to keep elements in the same order they are inserted. 


6. Synchronization
None of these map are synchornized. 


7. Usage
LinkedHashMap also provides a great starting point for creating a Cache object by overriding the removeEldestEntry() method. This lets you create a Cache object that can expire data using some criteria that you define.


And, here is a nice table of all the difference between HashMap, LinkedHashMap, Hashtable, and TreeMap in Java

Difference between HashMap, LinkedHashMap and TreeMap in Java

That's all about difference between HashMap, LinkedHashMap and TreeMap in Java. These differences includes both functional and non-functional differences between them. If you know them well, it will be easy for you to choose right kind of Map depending upon situation.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.