What is the Difference Between HashMap and LinkedHashMap

The main difference between HashMap and LinkedHashMap is that HashMap does not maintain the order of data insertion while LinkedHashMap maintains the order of data insertion.

The map is a commonly used data structure. Two implementations of Map in Java Standard Edition are HashMap and LikedHashMap. HashMap is implemented as a hash table. It has no ordering on keys or values. On the other hand, LinkedHashMap maintains the order of data insertion.

Key Areas Covered

1. What is HashMap
     – Definition, Functionality
2. What is LinkedHashMap
     – Definition, Functionality
3. What is the Difference Between HashMap and LinkedHashMap
     – Comparison of Key Differences

Key Terms

Hashmap, Hash Table, LinkedHashMap, Map

Difference Between HashMap and LinkedHashMap - Comparison Summary

What is HashMap

HashMap is a class that implements the Map interface using a hash table. It extends the AbstractMap and the AbstractMap implements the Map interface. A HashMap contains key-value pairs. It can consist of unique keys. HashMap is not synchronized, and it does not maintain the data inserted order. An example program with HashMap is as follows.

Difference Between HashMap and LinkedHashMap

Figure 1: Program with HashMap

In the above program,  the ‘employees’ is an object of HashMap. It can store multiple items with integer type key and String type value. The put method allows inserting items to the HashMap. The for loop with the Map.Entry is used to iterate through all the items in ‘employees’. The getKey method displays the keys while the getValues method prints the values corresponding to those keys.

HashMap does not print the elements according to the inserted order. Moreover, although there are two records with the same details, it only prints one of them. That is because HashMap contains only unique keys.

What is LinkedHashMap

LinkedHashMap class is Hashtable and Linked list implementation of the Map interface. It has a predictable iteration order. It inherits the HashMap class and implements the Map interface. LinkedHashMap also has key-value pairs and only contains unique elements. But, it is not synchronized.

An example is as follows:

Main Difference - HashMap vs LinkedHashMap

Figure 2: Program with LinkedHashMap

In the above program, the ‘employees’ is an object of LinkedHashMap. It can store multiple items with integer type key and String type value. The put method allows inserting items to the LinkedHashMap. The for loop with the Map.Entry help to iterate through all the items in ‘employees’. The getKey method displays the keys while the getValues method prints the values corresponding to those keys.

LinkedHashMap prints the elements according to the inserted order. Moreover, there are two records with the same details, but it only prints one of them. That is because LinkedHashMap contains only unique keys.

Difference Between HashMap and LinkedHashMap

Definition

HashMap is a class that helps to create a collection which inherits the AbstractMap class and implements the Map interface while LinkedHashMap is a class that is an implementation of both Hashtable and Linked List of the Map interface with predictable iteration order. Thus, this is the main difference between HashMap and LinkedHashMap.

Insertion Order

Furthermore, another difference between HashMap and LinkedHashMap is that HashMap does not maintain the order of data insertion while LinkedHashMap maintains the order of data insertion.

Association with the Collection Interface

Moreover, HashMap extends the AbstractMap and AbstractMap implements the Map interface. In contrast, LinkedHashMap extends HashMap; HashMap extends AbstratHashMap and AbstractHashMap implements Map interface.

Conclusion

HashMap and LinkedHashMap are two implementations of Map interface. The main difference between HashMap and LinkedHashMap is that HashMap does not maintain the order of data insertion while LinkedHashMap maintains the order of data insertion.

Reference:

1. “HashMap in Java – Javatpoint.” Www.javatpoint.com, Available here.
2. “LinkedHashMap in Java – Javatpoint.” Www.javatpoint.com, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply