← Back to Core Java Course | Chapter 11: Collections | Lesson 10 of 17

Java LinkedHashMap

LinkedHashMap combines a hash table with an internal linked list, giving it the fast lookups of a HashMap along with a predictable, insertion-ordered iteration.

Introduction to LinkedHashMap

LinkedHashMap is an implementation of Map that combines a hash table with an internal doubly-linked list, giving it both the fast lookup speed of a HashMap and a predictable iteration order.

Example: Introduction to LinkedHashMap

java
import java.util.LinkedHashMap;
public class Main {
	public static void main(String[] args) {
		LinkedHashMap<String, Integer> map = new LinkedHashMap<>(); // hash table + linked list
		map.put("a", 1);
		System.out.println(map);
	}
}

Insertion Order Preserved

Unlike HashMap, LinkedHashMap always iterates its entries in the exact order they were originally inserted, and updating the value of an existing key does not change that key's position in the order.

Example: Insertion Order Preserved

java
import java.util.LinkedHashMap;
public class Main {
	public static void main(String[] args) {
		LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
		map.put("banana", 1);
		map.put("apple", 2);
		map.put("banana", 99); // update doesn't move position
		System.out.println(map); // still banana, apple order
	}
}

LinkedHashMap vs HashMap

LinkedHashMap offers the same average-case performance as HashMap for put, get, and remove, at the cost of slightly more memory to maintain the linked list, in exchange for a guaranteed, predictable iteration order.

Example: LinkedHashMap vs HashMap

java
import java.util.LinkedHashMap;
import java.util.HashMap;
public class Main {
	public static void main(String[] args) {
		HashMap<String, Integer> hash = new HashMap<>();
		LinkedHashMap<String, Integer> linked = new LinkedHashMap<>();
		hash.put("z", 1); hash.put("a", 2);
		linked.put("z", 1); linked.put("a", 2);
		System.out.println(linked); // predictable order, unlike hash
	}
}

LinkedHashMap vs TreeMap

LinkedHashMap differs from TreeMap in what order it maintains: LinkedHashMap preserves the order entries were inserted in, while TreeMap always keeps entries sorted by key according to their natural ordering or a supplied Comparator.

Example: LinkedHashMap vs TreeMap

java
import java.util.LinkedHashMap;
import java.util.TreeMap;
public class Main {
	public static void main(String[] args) {
		LinkedHashMap<String, Integer> linked = new LinkedHashMap<>();
		TreeMap<String, Integer> tree = new TreeMap<>();
		linked.put("banana", 1); linked.put("apple", 2);
		tree.put("banana", 1); tree.put("apple", 2);
		System.out.println(linked); // insertion order
		System.out.println(tree); // sorted by key
	}
}

When to Use LinkedHashMap

LinkedHashMap is the right choice whenever fast key-based lookups are needed but the order entries were added in also matters, such as implementing a simple least-recently-used cache or preserving a visit history.

Example: When to Use LinkedHashMap

java
import java.util.LinkedHashMap;
public class Main {
	public static void main(String[] args) {
		LinkedHashMap<String, Integer> recentSearches = new LinkedHashMap<>();
		recentSearches.put("java", 1);
		recentSearches.put("python", 2);
		System.out.println(recentSearches); // fast lookup + preserved order
	}
}

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.