What is LinkedHashMap in Java?
- In Java, a LinkedHashMap is a specialized implementation of the Map interface that combines the features of a HashMap and a LinkedList.
- It is part of the java.util package and is used to store key-value pairs in a way that keeps up the insertion order of the components.
- Not at all like HashMap, which does not ensure any order, LinkedHashMap preserves the order in which components were inserted into the map.
- Internally, a LinkedHashMap uses a doubly-linked list to preserve the order of entries.
- This allows for predictable iteration order, where the components are returned within the same sequence as they were inserted.
- LinkedHashMap also provides the same constant-time (O(1)) performance for basic operations like get(), put(), and remove(), similar to HashMap, but with the added good thing about predictable order.

Key Characteristics of LinkedHashMap
1.Insertion Order:
- The essential feature of LinkedHashMap is its capacity to preserve the order of components based on their insertion order.
- Once you iterate through the map, the entries will be returned within the same sequence in which they were added.
2.Allows Null Values:
- Like HashMap, LinkedHashMap permits one null key and numerous null values.
3.Efficient Iteration:
- Due to the fundamental doubly-linked list structure, LinkedHashMap gives proficient iteration over the map’s entries whereas protecting their order.
4.Performance:
- It offers comparative performance to HashMap for essential operations.
- The average time complexity for get(), put(), and remove() is O(1). In any case, there’s a slight overhead for keeping up the linked list, so LinkedHashMap may be marginally slower than HashMap in a few cases.
5.Thread Safety:
- Like HashMap, LinkedHashMap isn’t thread-safe.
- In case thread security is required, you’ll use Collections.synchronizedMap() or select a ConcurrentHashMap.
Types of LinkedHashMap in Java
1.Standard LinkedHashMap:
- This is often the default implementation where the order of components is maintained based on their insertion order.
- Once you iterate over the map, it returns the components within the correct order they were added.
- Example:
import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapExample {
public static void main(String[] args) {
// Creating a LinkedHashMap
Map<String, Integer> map = new LinkedHashMap<>();
map.put(“Apple”, 3);
map.put(“Banana”, 2);
map.put(“Orange”, 5);
// Iterating over the map
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + “: ” + entry.getValue());
}
}
}
- Output:
Apple: 3
Banana: 2
Orange: 5
2.LinkedHashMap with Access Order (Least Recently Used – LRU):
- LinkedHashMap also permits for keeping up order based on access instead of insertion.
- This may be used to form a Least As of late Used (LRU) Cache, where components that are accessed most as of late are moved to the end of the list.
- You’ll enable this behavior by setting the accessOrder flag to true when making the LinkedHashMap.
- When this feature is enabled, the iteration order of the map is decided by the order of access instead of the order of insertion.
- Example:
import java.util.LinkedHashMap;
import java.util.Map;
public class LRUCacheExample {
public static void main(String[] args) {
// Creating a LinkedHashMap with access order
Map<String, Integer> lruCache = new LinkedHashMap<>(16, 0.75f, true);
lruCache.put(“A”, 1);
lruCache.put(“B”, 2);
lruCache.put(“C”, 3);
// Access some elements
lruCache.get(“A”);
lruCache.get(“B”);
// Adding a new entry which will push the least recently used element (“C”) out
lruCache.put(“D”, 4);
// Printing the cache entries
System.out.println(“Cache content: ” + lruCache);
}
}
- Output:
Cache content: {A=1, B=2, D=4}
Advantages of LinkedHashMap in Java
1.Maintains Insertion Order:
- This is the biggest advantage of LinkedHashMap over HashMap.
- In case you would like to protect the order in which components are added, LinkedHashMap is the perfect choice.
2.Efficient Iteration:
- Since of the linked list structure, LinkedHashMap guarantees that iteration through the map’s entries is quick and predictable.
3.Customizable Access Order:
- The capacity to use the map in access order empowers the creation of valuable data structures like LRU caches, which are used in scenarios where old or infrequently used information has to be discarded.
Example: Using LinkedHashMap as LRU Cache
Below is a simple example of using LinkedHashMap for creating an LRU Cache:
import java.util.LinkedHashMap;
import java.util.Map;
public class LRUCache {
private Map<Integer, Integer> cache;
public LRUCache(int capacity) {
cache = new LinkedHashMap<>(capacity, 0.75f, true);
}
public int get(int key) {
return cache.getOrDefault(key, -1); // Return -1 if key not found
}
public void put(int key, int value) {
if (cache.size() >= 3) {
int oldestKey = cache.keySet().iterator().next(); // Remove the first inserted key
cache.remove(oldestKey);
}
cache.put(key, value);
}
public void display() {
System.out.println(cache);
}
public static void main(String[] args) {
LRUCache lruCache = new LRUCache(3);
lruCache.put(1, 10);
lruCache.put(2, 20);
lruCache.put(3, 30);
lruCache.display(); // {1=10, 2=20, 3=30}
lruCache.get(2);
lruCache.put(4, 40); // Should remove key 1
lruCache.display(); // {3=30, 2=20, 4=40}
}
}
- Output:
{1=10, 2=20, 3=30}
{3=30, 2=20, 4=40}