In this article, we will discuss difference between HashMap and LinkedHashMap classes in detail i.e.; HashMap vs LinkedHashMap
Lets us move on and discuss key differences between these 2 Map implemented classes
1. HashMap v/s LinkedHashMap:
HashMap | LinkedHashMap |
Uses hash table to store map entries (i.e.; key-value pairs) | Uses combination of hash table + LinkedList to store map entries (i.e.; key-value pairs) |
Doesn’t maintain insertion-order i.e.; while iterating through HashMap, we will get map entries in random-order | Since, it uses doubly-linked list to store map entries (i.e.; key-value pairs), maintains insertion-order |
This is introduced in the original collection framework in Java 1.2 version | This is introduced in Java 1.4 version |
Q) When to use HashMap ?
- HashMap stores key-value pairs which uses hashing technique to store key-value pairs
- So, search operation is faster
- So, if business requirement is to store key-value pairs for faster search operation or more number of search operation on the baisis of keys; without concerning insertion-order
- Then, HashMap is the very apt choice
Q) When to use LinkedHashMap ?
- This is exactly same as that of HashMap, but underlying data structure to hold key-value pairs is different
- It uses doubly-linked list which allows to hold key-value pairs as per insertion-order
- So, if business requirement is to store key-value pairs for faster search operation or more number of search operation concerning/maintaining insertion-order
- Then, LinkedHashSet is the very apt choice which maintains insertion-order
- So while iterating through LinkedHashSet, we will get map entry (Key-Value pairs) as per insertion-order, as against random-order in HashMap
2. Map programs using HashMap & LinkedHashMap :
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));
}
}
}
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
Explanation:
- Sometime, iterating HashMap produces insertion-order but we can’t predict its actual order
- Because on different run/execution it produces results in different order
- That’s HashMap stores entries in random-order
- Whereas LinkedHashMap stores entries as per insertion-order, as its underlying data-structure is doubly LinkedList
Related Articles:
- Map interface
- Entry interface
- HashMap class
- LinkedHashMap class
- IdentityHashMap class
- WeakHashMap class
- SortedMap interface
- NavigableMap interface
- TreeMap class
- Hashtable class
- HashMap vs LinkedHashMap
- HashMap v/s LinkedHashMap v/s TreeMap
- HashMap v/s HashSet
- HashMap v/s Hashtable
- Properties class
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/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
Happy Coding !!
Happy Learning !!