Difference between Hashmap and LinkedHashMap in Java

We all use java.util.Map interface in daily programming. java.util.* package provides various concrete implementation i.e classes of Map interface. Today we will understand difference between HashMap and LinkedHashMap which are concrete class implementation of Map interface and we will also understand various aspects which will help you select between HashMap and LinkedHashMap while programming.

  HashMap LinkedHashMap
Order If you are using HashMap, it does not assure the order of insertion while traversal. If you are using LinkedHashMap, it guarantees the insertion order while traversal.
Memory Memory consumption of HashMap is less as compared to LinkedHashMap. As LinkedHashMap maintains the insertion order it consumes more memory as compared to HashMap.
Implementation HashMap uses hashtable internally to store key. LinkedHashMap internally implements hashtable and doubly LinkedList to keep order of elements inserted.

 

Let us check the behaviour of HashMap and LinkedHashMap by below example

public class PrivateStaticExample {

	public static void main(String[] args) throws Exception {
		HashMap<String, String> hm = new HashMap<String, String>();
		hm.put("One", "1");
		hm.put("Two", "2");
		hm.put("Three", "3");
		hm.put("Four", "4");
		hm.put("Five", "5");
		System.out.println("HashMap iterationing --- Results");
		for (String mapObj : hm.keySet()) {  
            System.out.println("Key: " + mapObj + " -- Value: "+ hm.get(mapObj));  
        }  
		
		LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();
		System.out.println("LinkedHashMap iterationing --- Results");
		lhm.put("One", "1");
		lhm.put("Two", "2");
		lhm.put("Three", "3");
		lhm.put("Four", "4");
		lhm.put("Five", "5");
		for (String mapObj : lhm.keySet()) {  
            System.out.println("Key: " + mapObj + " -- Value: "+ hm.get(mapObj));  
        }  
	}
}

Output

HashMap iterationing --- Results
Key: Five -- Value: 5
Key: One -- Value: 1
Key: Four -- Value: 4
Key: Two -- Value: 2
Key: Three -- Value: 3
LinkedHashMap iterationing --- Results
Key: One -- Value: 1
Key: Two -- Value: 2
Key: Three -- Value: 3
Key: Four -- Value: 4
Key: Five -- Value: 5

You can see in above example that HashMap does not maintain the insertion order whereas the LinkedHashMap maintains the insertion order while iterating through it.

When should you use LinkedHashMap?

I can remember an instance where I had to fetch some data from an external source and stored it to a Map and iterate the map to remove some keys which were not present in internal system in which I was fetching data. As other programmers, my first choice was HashMap. I used HashMap to store the data. Further, I got to know that the sequence in which the external source sent the data should be maintained at other end. This made a chaotic situation as I was iterating over the Map and performing some operation. As HashMap does not maintain the order of insertion while iteration, I have to use some other Map implementation. After looking into Javadocs, I found that LinkedHashMap maintains the order of insertion which suffice my requirement. 

Hence, if you require the order of insertion while iteration you can go for LinkedHashMap and if order of insertion is not required you can opt for HashMap.

In this article you learnt HashMap vs LinkedHashMap and their usage in application development.

Article tagged as
Author
Author: Amit Gupta
Published On: 12/03/2016
Last revised On: 18/09/2016
View all articles by Amit Gupta

Share this post

Comments

Comments
comments powered by Disqus

Navigation

Social Media