HashMap vs. LinkedHashMap in Java

Both HashMap and LinkedHashMap are implementations of the Map interface in Java, which store key-value pairs. While they share many similarities due to their shared interface and parent class (AbstractMap), they have key differences in how they store and manage the order of their elements.


HashMap

HashMap is a widely-used implementation of the Map interface that stores elements in a hash table. It uses a hashing mechanism to store the key-value pairs, which allows for fast retrieval of values when the key is known.

Characteristics

  • Order: HashMap does not maintain any order for its entries. The order of the elements is unpredictable and can change when the map is modified.
  • Null Values: HashMap allows one null key and multiple null values.
  • Performance: HashMap provides constant-time performance (O(1)) for basic operations like get and put, assuming the hash function disperses elements properly among the buckets.
  • Data Structure: The underlying data structure is a hash table.
Code Example
Output
Apple = 10
Cherry = 30
Banana = 20

LinkedHashMap

LinkedHashMap extends HashMap and maintains a linked list of the entries in the map. This linked list defines the iteration ordering, which is the order in which keys were inserted into the map (insertion-order).

Characteristics

  • Order: LinkedHashMap maintains insertion order. If you iterate through the map, the keys will be returned in the order in which they were inserted.
  • Null Values: Like HashMap, LinkedHashMap allows one null key and multiple null values.
  • Performance: Slightly slower than HashMap due to the additional overhead of maintaining the linked list, but still provides constant-time performance (O(1)) for basic operations like get and put.
  • Data Structure: The underlying data structure combines a hash table with a doubly-linked list that runs through all of its entries.
Code Example
Output
Apple = 10
Banana = 20
Cherry = 30

Key Differences Between HashMap and LinkedHashMap

Key Differences Between HashMap and LinkedHashMap

FeatureHashMapLinkedHashMap
OrderDoes not maintain any orderMaintains insertion order
PerformanceSlightly faster (no order maintenance)Slightly slower (due to linked list)
Data StructureHash tableHash table + doubly-linked list
Use CaseUse when order does not matterUse when you need to preserve order

When to Use HashMap

  • Performance-Critical Operations: If you need the fastest possible retrieval and insertion times and don't care about the order of elements, HashMap is the better choice.
  • Large Data Sets: HashMap is often more suitable for very large data sets where the overhead of maintaining order is unnecessary.

When to Use LinkedHashMap

  • Preserving Insertion Order: If you need to maintain the order in which entries are added, LinkedHashMap is ideal.
  • Cache Implementation: LinkedHashMap is particularly useful for implementing caches with a predictable iteration order or for implementing LRU (Least Recently Used) caches by overriding its removeEldestEntry method.

Output

{A=1, B=2, C=3}
{C=3, A=1, D=4}

In this example, the LRUCache will automatically remove the least recently accessed entry once it reaches its capacity.