Java LinkedHashMap
In this page:
What is a LinkedHashMap?
A LinkedHashMap maintains an internal doubly-linked list running through all of its entries. Unlike a plain HashMap, whose iteration order is unspecified and can even change between runs, a LinkedHashMap preserves the order keys were originally inserted, making iteration predictable.
Example: What is a LinkedHashMap?
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("c", 3); map.put("a", 1); map.put("b", 2);
System.out.println(map); // preserves insertion order: c, a, b
}
}
Login to try C/C++/Java/PHP code in the editor
Access-Order Mode
You can configure a LinkedHashMap to use access-order mode instead of the default insertion-order mode, which automatically moves an entry to the end of the iteration order every time it's read, not just when it's written — the basic building block for an LRU eviction policy.
Example: Access-Order Mode
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<>(16, 0.75f, true); // access-order
map.put("a", 1); map.put("b", 2); map.put("c", 3);
map.get("a"); // moves "a" to the end
System.out.println(map);
}
}
Login to try C/C++/Java/PHP code in the editor
Building an LRU Cache
You can build a Least Recently Used (LRU) cache by extending LinkedHashMap in access-order mode and overriding its removeEldestEntry() method to return true once the map exceeds your chosen maximum size. The map then automatically evicts the oldest, least-recently-used entry for you on every insertion.
Example: Building an LRU Cache
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
LinkedHashMap<Integer, String> cache = new LinkedHashMap<>(16, 0.75f, true) {
protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) {
return size() > 2;
}
};
cache.put(1, "a"); cache.put(2, "b"); cache.put(3, "c"); // evicts 1
System.out.println(cache);
}
}
Login to try C/C++/Java/PHP code in the editor
Iteration and Performance
Iterating a LinkedHashMap is generally a bit faster than iterating a plain HashMap of the same size, since a LinkedHashMap's iteration time depends only on the number of entries actually present, whereas a HashMap's iteration time depends on the underlying table's full capacity.
Example: Iteration and Performance
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
for (int i = 0; i < 5; i++) map.put(i, "v" + i);
for (var entry : map.entrySet()) { /* iteration cost depends only on size, not capacity */ }
System.out.println("Entries: " + map.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Key Operations and Null Values
Like a standard HashMap, a LinkedHashMap supports exactly one null key and any number of null values, all while still faithfully preserving whichever ordering mode — insertion or access — it was configured with.
Example: Key Operations and Null Values
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put(null, "nullKeyValue");
map.put("a", null);
System.out.println(map);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: