HashMap and LinkedHashMap are the classes, quite similar to each other and are used for creating a map. HashMap class extends the AbstractMap class to use a hash table to store the elements in the map. LinkedHashMap class maintains the entries in the map based on their insertion order.
The feature that distinguishes HashMap and LinkedHashMap from each other is that Hashmap does not maintain the order of the stored entries in a map. On the other hand, LinkedHashMap uses a hybrid data structure to maintain the order of entries in which they were inserted. In the Comparison Chart below I explored some other differences between HashMap and LinkedHashMap just have a look.
Content: HashMap Vs LinkedHashMap
Comparison Chart
Basis for Comparison | HashMap | LinkedHashMap |
---|---|---|
Basic | Insertion order in HashMap is not preserved. | Insertion order is preserved in LinkedHashMap. |
Data Structure | HashMap uses HashTable to store maps. | LinkedHashMap uses HashTable along with Linked List to store map. |
Extends/Implements | HashMap extends AbstractMap and implements Map interface. | LinkedHashMap extends Hashmap. |
Version | HashMap was introduced in JDK 2.0. | LinkedHashMap was introduced in JDK 4.0. |
Overhead | Comparatively less overhead. | Comparatively more overhead because it has to maintain the order of the map entries. |
Definition of HashMap
HashMap is a class that is used to create a map. It implements Map Interface. It also extends the AbstractMap class so that it can use a hash table to store the entries in the map. Entries of the map is a <key,value> pair where each key is associated with the value. The key in the entry is used for retrieving the value hence, the key must be unique.
That’s why duplicate keys are not allowed in the HashMap. But the key in the each entry of the map may have different type i.e. the keys in the map created by HashMap can be heterogeneous. The data structure used by the HashMap to store a map is a hash table.
The insertion order of the entries in the HashMap is not preserved. The insertion of entries in the map created using HashMap is based on the hash code calculated by the keys in entries. If by mistake you entered a duplicate key in the HashMap, it will replace the previous value of that key with the new value proposed and will return the old value. If no duplicate key is used and no replacement has taken place, the key always returns Null.
Let us see how to add the entries to the hash map with the following example.
Hashmap hm = new Hashmap(); hm.put("Ajay", 275); hm.put("Vijay", 250); hm.put("Jonny", 150); hm.put("Jordan", 200); System.out.println( hm); /*output*/ {Vijay=250, Jonny=150, Ajay=275, Jordan=200}
As in the above code, you can see I created an object of HashMap and added the entries using puts method and when I printed the HashMap object, the entries are not printed in the order they were inserted. Hence, you can not pretend the order of the entries in the HashMap will return.
HashMap uses all the methods of Map interface and AbstractMap class and does not introduce any new method; it has its own constructors. The default capacity of the hash map is 16 and default fill ratio is 0.75.
Definition of LinkedHashMap
LinkedHashMap is also a class use to create a map. LinkedHashMap extends the HashMap class and was introduced later to HashMap in JDK version 4.0. Being the child class of HashMap class LinkedHashMap is exactly same as the HashMap class including the constructors and methods. But, LinkedHashMap differs in a sense that it maintains the order of the insertion of the entries in the map. The data structure that is used by LinkedHashMap to store the map is linked list and hash table.
In addition to the methods inherited by HashMap, LinkedHashMap introduces one new method that is removeEldestEntry( ). This method is used to remove the oldest entry in the map. The default capacity of the LinkedHashMap is 16, and the default fill ratio is 0.75 which is also similar to the HashMap class.
Key Differences Between HashMap and LinkedHashMap in Java
- The most important difference is that insertion order of the HashMap is not preserved whereas, the insertion order of the LinkedHashMap is preserved.
- The data structure used by HashMap to store the elements of the map is Hashtable. On the other hand, the data structure used by the LinkedHashMap is Linked list and Hashtable.
- HashMap class extends AbstractMap class and implements the Map interface. However, the LinkedHashMap class is a child class of HashMap class i.e. LinkedHashMap class extends HashMap class.
- HashMap class was introduced in the JDK 2.0 version. The LinkedHashMap class was introduced later in JDK 4.0 version.
- Comparatively LinkedHashMap class has more overhead than HashMap as it has to maintain the order of the elements inserted in the map.
Conclusion
LinkedHashMap must only be used where we are concerned about the sequence of the elements inserted in the map.
Leave a Reply