🔍 What is LinkedHashMap
?
LinkedHashMap
is a hash table + doubly linked list implementation of theMap
interface.
It maintains the insertion order (or access order) of entries, unlike regularHashMap
.
🧩 Inherited from HashMap
✅
Feature | Description |
---|---|
🔑 Fast O(1) access | Uses hash buckets for quick lookup |
🧠 Uses .hashCode() + .equals() | For hashing and key comparison |
✅ Allows null key and values | Just like HashMap |
🔐 Not thread-safe | Same as HashMap |
So performance-wise, it works just like HashMap
for lookup, put, remove — fast and efficient.
🧩 Added from LinkedList
✅
Feature | Description |
---|---|
🪝 Doubly linked list | Maintains insertion or access order of entries |
📜 Predictable iteration | Iterates in the order items were added or accessed |
🔁 Custom order option | Can be set to access-order (LRU-style) if needed |
Internally,
LinkedHashMap
adds a linked list of entries that tracks the order of insertion (or access).
👀 Constructor Example
LinkedHashMap<String, String> map = new LinkedHashMap<>(
16, 0.75f, false // false = insertion order, true = access order
);
🚀 Real Use Cases
Use Case | Why It Fits |
---|---|
Ordered caching | Keeps insertion or access order |
Building LRU cache | With accessOrder = true + removeEldestEntry() override |
Exporting JSON/XML | Keys stay in the same order they were inserted |
Pretty logging/debugging | Easier to read when order is preserved |
🧠 TL;DR:
LinkedHashMap
=
HashMap’s speed 🔥
LinkedList’s order tracking 📜
→ Perfect when you want fast lookup and predictable order.
Let’s look at a practical example of using LinkedHashMap
and iterating over it to see how it preserves order — either insertion order or access order.
🔹 Example 1: Iteration in Insertion Order (default)
import java.util.LinkedHashMap;
import java.util.Map;
public class InsertionOrderExample {
public static void main(String[] args) {
Map<String, Integer> map = new LinkedHashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
System.out.println("Iteration in insertion order:");
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " => " + entry.getValue());
}
}
}
🧾 Output:
Iteration in insertion order:
apple => 1
banana => 2
cherry => 3
✅ Order is exactly how we inserted the items.
🔸 Example 2: Iteration in Access Order
import java.util.LinkedHashMap;
import java.util.Map;
public class AccessOrderExample {
public static void main(String[] args) {
Map<String, Integer> map = new LinkedHashMap<>(16, 0.75f, true); // accessOrder = true
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
// Access some entries
map.get("banana");
map.get("apple");
System.out.println("Iteration in access order:");
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " => " + entry.getValue());
}
}
}
🧾 Output:
Iteration in access order:
cherry => 3
banana => 2
apple => 1
✅ Notice: cherry
comes first because it wasn’t accessed.banana
and apple
moved to the end after being read.
🧠 TL;DR:
- ✅
LinkedHashMap
preserves order:- By insertion (default) 🧱
- Or by access (if configured) 🔄
- Perfect when you need both speed of HashMap and predictable iteration order.