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.7, maximum of 1 NULL allowed as 1st elementValue: 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  Source: Team BenchResources.Net

 

Map Program using HashMap, LinkedHashMap and TreeMap :

MapExample.java

package in.bench.resources.collection;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MapExample {

	public static void main(String[] args) {

		// 1. Creating HashMap object
		Map<Integer, String> hashMap = 
				new HashMap<Integer, String>();

		// 1.1 add few entries
		hashMap.put(40, "RajKumarHirani");
		hashMap.put(50, "SanjayLeelaBanshali");
		hashMap.put(20, "Shankar");
		hashMap.put(10, "ManiRatnam");
		hashMap.put(30, "RajaMouli");

		// 1.2 get entrySet()
		Set<Map.Entry<Integer, String>> entries = hashMap.entrySet();	

		System.out.println("Displaying HashMap entries"
				+ " in Random-order : \n");

		// 1.3 Iterate using enhanced for-Each loop
		for(Map.Entry<Integer, String> entry : entries) {
			System.out.println("Rank : " + entry.getKey() 
					+ "\t Director : " + entry.getValue());
		}

		// 2. Creating LinkedHashMap object
		Map<Integer, String> linkedHashMap = 
				new LinkedHashMap<Integer, String>();

		// 2.1 add few entries
		linkedHashMap.put(20, "Shankar");
		linkedHashMap.put(50, "SanjayLeelaBanshali");
		linkedHashMap.put(40, "RajKumarHirani");
		linkedHashMap.put(10, "ManiRatnam");
		linkedHashMap.put(30, "RajaMouli");


		// 2.2 get keySet()
		Set<Integer> keys = linkedHashMap.keySet();

		System.out.println("\nDisplaying LinkedHashMap entries"
				+ " as per Insertion-order : \n");

		// 2.3 Iterate using enhanced for-Each loop
		for(Integer rank : keys) {
			System.out.println("Rank : " + rank 
					+ "\t Director : " + linkedHashMap.get(rank));
		}

		// 3. Creating HashSet object
		Map<Integer, String> treeMap = 
				new TreeMap<Integer, String>();

		// 3.1 add few entries
		treeMap.put(40, "RajKumarHirani");
		treeMap.put(20, "Shankar");
		treeMap.put(50, "SanjayLeelaBanshali");
		treeMap.put(10, "ManiRatnam");
		treeMap.put(30, "RajaMouli");


		// 3.2 get keySet()
		Set<Integer> ranks = treeMap.keySet();

		System.out.println("\nDisplaying TreeMap entries"
				+ " as per ASC Sorting-order : \n");

		// 3.3 Iterate using enhanced for-Each loop
		for(Integer rank : ranks) {
			System.out.println("Rank : " + rank 
					+ "\t Director : " + treeMap.get(rank));
		}
	}
}

Output:

Displaying HashMap entries in Random-order : 

Rank : 50	 Director : SanjayLeelaBanshali
Rank : 20	 Director : Shankar
Rank : 40	 Director : RajKumarHirani
Rank : 10	 Director : ManiRatnam
Rank : 30	 Director : RajaMouli

Displaying LinkedHashMap entries as per Insertion-order : 

Rank : 20	 Director : Shankar
Rank : 50	 Director : SanjayLeelaBanshali
Rank : 40	 Director : RajKumarHirani
Rank : 10	 Director : ManiRatnam
Rank : 30	 Director : RajaMouli

Displaying TreeMap entries as per ASC Sorting-order : 

Rank : 10	 Director : ManiRatnam
Rank : 20	 Director : Shankar
Rank : 30	 Director : RajaMouli
Rank : 40	 Director : RajKumarHirani
Rank : 50	 Director : SanjayLeelaBanshali

 

Factors to consider while discussing any collection class

We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,

  • Underlying data structure
  • Duplicates are allowed or Not
  • Insertion order is maintained or Not
  • Whether NULL insertion is possible or Not
  • If possible, how many NULL values can be inserted
  • Whether collection class provide sorting, by default
  • Is there any way to apply customized sorting
  • Performance, while dealing with retrieval or manipulation (addition/deletion)
  • By default, all methods are synchronized or Not

 

References:

 

Happy Coding !!
Happy Learning !!

HashMap v/s HashSet
TreeMap class