Tech 

Difference between HashMap and LinkedHashMap in Java




HashMap and LinkedHashMap are very similar classes to each other and are used to create a map. The HashMap class extends the AbstractMap class with a hash table to store the elements in the map. The LinkedHashMap class manages the entries in the map based on their insertion order. The feature that differentiates HashMap and LinkedHashMap from each other is that hash map does not preserve the order of entries saved in a map. another hand used LinkedHashMap a hybrid data structure to preserve the order of entries in which they were entered. In the comparison table below, I’ve looked at some other differences between HashMap and LinkedHashMap.

Comparison table

basis of comparison HashMap LinkedHashMap
basic The insertion order in HashMap is not preserved. Insert order is preserved in LinkedHashMap.
data structure HashMap uses HashTable to store maps. LinkedHashMap uses HashTable along with the Linked List to store the map.
Extended / Implemented HashMap extends AbstractMap and implements the Map interface. LinkedHashMap extends the Hashmap.
execution HashMap was introduced in JDK 2.0. LinkedHashMap was introduced in JDK 4.0.
the overload Comparatively less effort. Comparatively more effort, as the order of map entries must be preserved.

Definition of HashMap

HashMap is a class used to create a map. it implements Map Interface. In addition, the abstract map Extended class so that a hash table can be used to store the entries in the map. Card entries are paired with each key associated with the value. The key in the input is used to get the value. Therefore, the key must be unique. Therefore, duplicate keys are not allowed in HashMap. However, the key in each map entry can be of a different type, that is, the keys in the map created by HashMap can be heterogeneous. The data structure used by HashMap to store a map is a hash table.

The order of entries in HashMap is not preserved. Inserting entries into the map created with HashMap is based on the hash code calculated from the keys in the entries. If you accidentally entered a duplicate key in HashMap, the previous value of that key will be replaced with the new proposed value and the old value will be returned. If no duplicate keys are used and no replacements have taken place, the key will always return null. The example below shows how to add the entries to the hash map.

 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 can be seen in the code above, I created a HashMap object and added the inputs using the put method. When I print the HashMap object, the entries are not printed in the order they were entered. Therefore, you cannot specify that the order of entries in the HashMap be returned. HashMap uses all the methods of the Map interface and the AbstractMap class and does not introduce any new methods. It has its own designers. The default capacity of the hash map is 16 and the default fill level is 0.75 .

Definition of LinkedHashMap

LinkedHashMap is also a class used to create a map. LinkedHashMap extends the HashMap class and was later introduced to HashMap in JMK version 4.0. As a subclass of the HashMap class, LinkedHashMap is exactly the same as the HashMap class, including constructors and methods. However, LinkedHashMap differs in that it preserves order when inserting entries into the map. The data structure used by LinkedHashMap to store the map is a linked list and hash table .

In addition to the methods inherited from HashMap, LinkedHashMap introduces a new method, the removeEldestEntry() is . This method removes the oldest entry in the map. The default capacity of LinkedHashMap is 16 and the default padding level is 0.75, which is also similar to the HashMap class.

Main Differences Between HashMap and LinkedHashMap in Java

  1. The main difference is that the HashMap insertion order is not maintained while the LinkedHashMap insertion order is held .
  2. The data structure used by HashMap to store map elements is hash table . The data structure used by LinkedHashMap is against it linked list and hash table .
  3. The HashMap class extends the abstract map class and implements the map Interface. However, the LinkedHashMap class is a subclass of the class HashMap class, that is, the LinkedHashMap class extends the HashMap class.
  4. The HashMap class was created in JDK 2.0- introduced version. The LinkedHashMap class was later introduced in JDK 4.0- introduced version.
  5. The comparable LinkedHashMap class has more overhead than HashMap because it must preserve the order of elements inserted into the map.

Conclusion:

LinkedHashMap can only be used when it comes to the order of elements inserted in the map.

Related posts

Leave a Comment