Saltar al contenido
Contact : alejandrasalcedo0288@gmail.com

Difference between HashMap and LinkedHashMap in Java


HashMap and LinkedHashMap are the classes, very similar to each other and are used to create a map. The HashMap class extends the AbstractMap class to use a hash table to store the elements on the map. The LinkedHashMap class keeps the entries on the map based on their insertion order. The characteristic that distinguishes HashMap and LinkedHashMap from each other is that Hashmap does not maintain the order of entries stored on a map. On the other hand, LinkedHashMap uses a hybrid data structure to maintain the order of the entries in which they were inserted. In the following comparison chart, I explore some other differences between HashMap and LinkedHashMap.

Comparative graph

Basis for comparisonHashMapLinkedHashMap
BASICThe order of insertion in HashMap is not preserved.The insertion order is preserved in LinkedHashMap.
Data structureHashMap uses HashTable to store maps.LinkedHashMap uses HashTable along with the linked list to store the map.
Extend / ImplementHashMap extends AbstractMap and implements the Map interface.LinkedHashMap extends Hashmap.
VersionHashMap was introduced in JDK 2.0.LinkedHashMap was introduced in JDK 4.0.
General expensesComparatively less overhead.Comparatively more overhead because you have to maintain the order of the map entries.

HashMap definition

HashMap is a class that is used to create a map. Implements Map Interface. It also extends the class Abstractmap so you can use a hash table to store the entries on the map. The map entries are a pair where each key is associated with the value. The key in the entry is used to retrieve the value, therefore, the key must be unique. That is why duplicate keys are not allowed in the HashMap. But the key in each map entry can have 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 insertion 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 the entries. If you mistakenly enter a duplicate key in the HashMap, replace the previous value of that key with the proposed new value and return the previous value. If a duplicate key is not used and no substitution has been made, the key always returns Null. Let's 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); / * salida * / {Vijay = 250, Jonny = 150, Ajay = 275, Jordan = 200} 

As in the previous code, you can see that you created a HashMap object and added the entries using the placement method and when you print the HashMap object, the entries are not printed in the order in which they were inserted. Therefore, you cannot pretend that the order of the entries in the HashMap return. HashMap uses all the methods of the Map interface and the AbstractMap class and does not introduce any new methods; It has its own builders. The default hash map capacity is 16 and the default fill ratio is 0.75 .

LinkedHashMap definition

LinkedHashMap is also a class use to create a map. LinkedHashMap extends the class Hashmap and was later introduced to HashMap in version 4.0 of JDK. Being the secondary class of the HashMap LinkedHashMap class is exactly the same as the HashMap class, including constructors and methods. But, LinkedHashMap differs in the sense that it maintains the order of insertion of entries on the map. The data structure that LinkedHashMap uses to store the map is the linked list and the hash table .

In addition to the methods inherited by HashMap, LinkedHashMap introduces a new method that is removeEldestEntry () . This method is used to remove the oldest entry on the map. The default LinkedHashMap capacity 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

  1. The most important difference is that the order of insertion of HashMap not preserved, while the LinkedHashMap insertion order is conserve .
  2. The data structure used by HashMap to store map elements is Hashtable . On the other hand, the data structure used by LinkedHashMap is the linked list Y Hashtable .
  3. The HashMap class extends the class Abstractmap and implements the interface Map . However, the LinkedHashMap class is a secondary class of the class Hashmap, that is, the LinkedHashMap class extends the HashMap class.
  4. The HashMap class was introduced in the version JDK 2.0 . The LinkedHashMap class was introduced later in the version JDK 4.0 .
  5. Comparatively, the LinkedHashMap class has more overhead than HashMap, since it must maintain the order of the elements inserted in the map.

Conclusion:

LinkedHashMap should only be used when we are concerned about the sequence of the elements inserted in the map.

Rate this post