HashMap v/s LinkedHashMap v/s TreeMap

In this article, we will compare important implementation classes of Map i.e.; HashMap v/s LinkedHashMap v/s TreeMap

So let’s us discuss in tabular format;

 

HashMap v/s LinkedHashMap v/s TreeMap:

HashMap LinkedHashMap TreeMap
Uses hash table to store key-value pairs (i.e.; map entries) where duplicate keys are NOT allowed Uses combination of (hash table + LinkedList) to store key-value pairs (i.e.; map entries) where duplicate keys are NOT allowed Uses Red-Black tree to store key-value pairs (i.e.; map entries) where duplicate keys are NOT allowed
Insertion order is NOT maintained, as it uses hashing technique to store key-value pairs (i.e.; map entries) Insertion order is maintained, as it uses doubly-linked list to store key-value pairs (i.e.; map entries) Insertion order is NOT maintained, as key-value pairs (i.e.; map entries) are stored according to some sorting order
HashMap doesn’t deal with  sorting order; but it can be converted to TreeMap to store key-value pairs (i.e.; map entries) in some sorting order

TreeMap ts = new TreeMap(hashMap);

LinkedHashMap doesn’t deal with  sorting order; but it can be converted to TreeMap to store key-value pairs (i.e.; map entries) in some sorting order

TreeMap ts = new TreeMap(linkedHashMap);

Keys in TreeMap are sorted, according to some sorting order; it could be either default natural sorting order or programmer defined customized sorting order
While iterating HashMap, we will get items in random order While iterating LinkedHashMap, we will get items as per insertion order While iterating TreeMap, we will get items in sorted order;  either natural ordering or customized sorting order
This is introduced in original collection framework in Java 1.2 version This is introduced in Java 1.4 version This is also introduced in original collection framework in Java 1.2 version
Key: Allows NULL insertion but maximum of only one NULL value

Value: No upper limit for NULL values against any unique key

Key: Allows NULL insertion but maximum of only one NULL value

Value: No upper limit for NULL values against any unique key

Key: From Java 1.7 version, NULL is not allowed to insert;
But with Java version less than 1.6, only as 1st element allowed (for keys)

Value: No upper limit for NULL values against any unique key

 

Refer for detailed explanation:

  1. HashMap with example
  2. LinkedHashMap with example
  3. TreeMap with example
  4. HashMap v/s LinkedHashMap

 

025-map-interace-in-java 

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
https://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/sorted-map.html
https://docs.oracle.com/javase/7/docs/api/java/util/NavigableMap.html
https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html

 

Happy Coding !!
Happy Learning !!