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 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.
import java.util.HashMap;
import java.util.Map;
public class HashMapExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 10);
map.put("Banana", 20);
map.put("Cherry", 30);
// Iterating over HashMap
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
Apple = 10
Cherry = 30
Banana = 20
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).
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
.xxxxxxxxxx
import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapExample {
public static void main(String[] args) {
Map<String, Integer> map = new LinkedHashMap<>();
map.put("Apple", 10);
map.put("Banana", 20);
map.put("Cherry", 30);
// Iterating over LinkedHashMap
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
Apple = 10
Banana = 20
Cherry = 30
Feature | HashMap | LinkedHashMap |
---|---|---|
Order | Does not maintain any order | Maintains insertion order |
Performance | Slightly faster (no order maintenance) | Slightly slower (due to linked list) |
Data Structure | Hash table | Hash table + doubly-linked list |
Use Case | Use when order does not matter | Use when you need to preserve order |
HashMap
is the better choice.HashMap
is often more suitable for very large data sets where the overhead of maintaining order is unnecessary.LinkedHashMap
is ideal.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.xxxxxxxxxx
import java.util.LinkedHashMap;
import java.util.Map;
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
private final int capacity;
public LRUCache(int capacity) {
super(capacity, 0.75f, true);
this.capacity = capacity;
}
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > capacity;
}
public static void main(String[] args) {
LRUCache<String, Integer> cache = new LRUCache<>(3);
cache.put("A", 1);
cache.put("B", 2);
cache.put("C", 3);
System.out.println(cache); // Output: {A=1, B=2, C=3}
cache.get("A"); // A=1, is used so it is not removed from the cache.
cache.put("D", 4);
System.out.println(cache); // Output: {B=2, C=3, A=1} (B is removed)
}
}
{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.