HashMap and LinkedHashMap are two of the most common and general-purpose Map implementations in the Java platform. They are basically hash-based classes, quite similar to each other and are used for creating a Map. The Map interface is the last of the major Collections Framework interfaces which defines the operations that are supported by a set of key-to-value associations in which the keys are unique. These Map implementations are based on a hashing algorithm. While the class HashMap implements unordered maps, the class LinkedHashMap implements ordered maps. The LinkedHashMap implementation is a subclass of the HashMap class which means it inherits the features of class HashMap. There is not much difference between the two in terms of performance. Let’s take a look.
What is HashMap?
HashMap is one of the most common and among the four general-purpose implementations of the Map interface in Java based on a hashing algorithm. It is analogous to the set class HashSet, although, the elements are unordered in both classes. It is implemented as a hash table but unlike LinkedHashMap, it does not maintain any order on keys or values. In general, HashMap provides constant-time performance for put and get. The class is not thread-safe but it permits one null key and multiple null values. As it does not maintain an iterating order, it requires less memory.
What is LinkedHashMap?
LinkedHashMap is among the four general-purpose implementation of the Map interface which is a subclass of the HashMap class meaning it inherits its features. Although it’s very similar to HashMap in terms of performance, except it maintains insertion order of keys, either in order in which the keys are inserted into the Map or the order in which the entries are accessed in the Map. It refines the contract of its parent class by guaranteeing the order in which iterators returns its elements. However, it requires more memory than a HashMap because it maintains a doubly-linked list in Java.
Difference between HashMap and LinkedHashMap
-
Basics of HashMap Vs. LinkedHashMap
HashMap is a hash-based implementation of Map interface in Java. Maps are a collection of key-value pairs and are used when Lists are in ordered collection. HashMap is a Collection class that stores value in key-value pairs. In simple terms, it maps keys to values meaning it can locate a value based on a key. LinkedHashMap is a linked list implementation of the Map interface just like HashMap except it maintains orders of elements inserted into it. It is a subclass of HashMap which inherits its features. LinkedHashMap refines the contract of its parent class, HashMap, by guaranteeing the order in which iterators returns its elements.
-
Iteration Order
The key difference between HashMap and LinkedHashMap is order. Elements of a HashMap are not in order, totally random, whereas elements of LinkedHashMap are ordered. The entries of a LinkedHashMap are in key insertion order, which is the order in which the keys are inserted in the Map. This means the first key inserted into the Map is enumerated first, so does the value associated with it, and the last entry inserted in enumerated last. LinkedHashMap has a predictable iteration order which means it can also maintain its elements in access order, which is the order in which the entries are accessed.
-
Implementation
Both HashMap and LinkedHashMap classes use hashing to implement Map interface in Java except HashMap is implemented as a hash table whereas LinkedHashMap maintains a doubly linked list of Buckets running through all its entries. This is why LinkedHashMap requires more memory than HashMap because unlike HashMap, it maintains an order. It removes the chaotic ordering by HashMap, without incurring the additional cost that would have been incurred otherwise with TreeMap. Besides that, the LinkedHashMap class is very similar to the HashMap class in many aspects such as synchronization and null keys/values as both allow one null key and multiple null values.
-
Performance for HashMap Vs. LinkedHashMap
Although, both the classes provide comparable performance, the HashMap class is believed to be the preferred choice if ordering is not an issue because it does not guarantee as to the iterating order of the Map. Operations such as adding, removing, or finding entries based on a key are constant time, as they hash the key. So adding, removing, and finding entries in a LinkedHashMap can be slightly slower than in a HashMap because it maintains a doubly-linked list of Buckets in Java. Additionally, HashMap requires less memory than LinkedHashMap because no order is maintained.
HashMap vs. LinkedHashMap: Comparison Chart
Summary of HashMap Vs. LinkedHashMap
While both HashMap and HashMap classes are almost similar in performance, HashMap requires less memory than a LinkedHashMap because it does not guarantee the iterating order of the map, which makes adding, removing, and finding entries in a HashMap relatively faster than doing the same with a LinkedHashMap. However, the key difference between the two is order: the elements of a HashMap are not in order, while the elements of a LinkedHashMap are in key insertion order by default meaning the order in which the keys are inserted into the map. LinkedHashMap can also maintain its elements in access order meaning the order in which the entries are accessed. As with LinkedHashMap, a doubly-linked list has to be maintained, it has less performance than HashMap.