← Back to Advanced Java Course | Chapter 5: Collections Advanced | Lesson 2 of 4

Java LinkedHashMap

A LinkedHashMap is a map that remembers the order items were added, like a phone book that keeps entries in the order you wrote them. It can also be used to make a cache.
Syntax
java
LinkedHashMap<KeyType, ValueType> map = new LinkedHashMap<>();
map.put(key, value);  // iteration follows insertion order

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?

java
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
	}
}

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

java
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);
	}
}

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

java
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);
	}
}

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

java
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());
	}
}

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

java
// Import java.util.LinkedHashMap so it can be used by its short name
import java.util.LinkedHashMap;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new LinkedHashMap object and store it in map
		LinkedHashMap<String, String> map = new LinkedHashMap<>();
		map.put(null, "nullKeyValue");
		map.put("a", null);
		// Print a line to the console
		System.out.println(map);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting a LinkedHashMap to sort keys, when it keeps insertion order (or access order) and does not sort.
  2. Forgetting the third constructor argument true, as in new LinkedHashMap<>(16, 0.75f, true), so access-order mode is never enabled.
  3. Building an LRU cache without overriding removeEldestEntry, so old entries are never removed and the map keeps growing.
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.