Java LinkedHashMap
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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 try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting