LinkedHashMap in Java
LinkedHashMap in Java implements the Map interface of the Collections Framework. It stores key-value pairs while maintaining the insertion order of the entries. It maintains the order in which elements are added.
- Stores unique key-value pairs.
- Maintains insertion order.
- Allows one null key and multiple null values.
- It is not thread-safe; to synchronize it, use Collections.synchronizedMap().
Declaration of LinkedHashMap
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>
Here, K is the key Object type and V is the value Object type
- K: The type of keys in the map.
- V: The type of values mapped in the map.
import java.util.LinkedHashMap;
public class Geeks{
public static void main(String[] args){
// Create a LinkedHashMap of Strings (keys) and Integers (values)
LinkedHashMap<String, Integer> lhm = new LinkedHashMap<>();
// Adding key-value pairs
lhm.put("Apple", 50);
lhm.put("Banana", 30);
lhm.put("Mango", 70);
lhm.put("Orange", 40);
// Displaying the LinkedHashMap
System.out.println("LinkedHashMap: " + lhm);
}
}
Output
LinkedHashMap: {Apple=50, Banana=30, Mango=70, Orange=40}
Internal Working of LinkedHashMap
LinkedHashMap extends HashMap and implements the Map interface:
public class LinkedHashMap extends HashMap implements Map
It stores data as nodes similar to a doubly-linked list, maintaining insertion order. Each node contains:

- Key: Since this class extends HashMap, the data is stored in the form of a key-value pair. Therefore, this parameter is the key to the data.
- Value: For every key, there is a value associated with it. This parameter stores the value of the keys. Due to generics, this value can be of any form.
- Next: Since the LinkedHashMap stores the insertion order, this contains the address to the next node of the LinkedHashMap.
- Previous: This parameter contains the address to the previous node of the LinkedHashMap.
Hierarchy of LinkedHashMap
It extends HashMap and maintains a doubly-linked list to preserve the insertion order of elements.

Constructors of LinkedHashMap Class
LinkedHashMap class provides various constructors for different use cases:
1. LinkedHashMap()
Creates an empty LinkedHashMap with default initial capacity (16) and load factor (0.75)
LinkedHashMap<K, V> lhm = new LinkedHashMap<>();
2. LinkedHashMap(int initialCapacity, float loadFactor)
Creates a LinkedHashMap with specified capacity and load factor.
LinkedHashMap<K, V> lhm = new LinkedHashMap<>(20, 0.75f);
3. LinkedHashMap(Map<? extends K,? extends V> map)
Creates a LinkedHashMap containing all elements from the specified map, maintaining their insertion order.
LinkedHashMap<K, V> lhm = new LinkedHashMap<>(existingMap);
Performing Various Operations on LinkedHashMap
Let’s see how to perform a few frequently used operations on the LinkedHashMap class instance.
1. Adding Elements in LinkedHashMap
We can add elements to a LinkedHashMap using the put() method, which retains the insertion order unlike HashMap.
import java.util.*;
class Geeks{
public static void main(String args[]) {
// Initialization of a LinkedHashMap
// using Generics
LinkedHashMap<Integer, String> lhm
= new LinkedHashMap<Integer, String>();
// Add mappings to Map
// using put() method
lhm.put(3, "Geeks");
lhm.put(2, "For");
lhm.put(1, "Geeks");
// Printing mappings to the console
System.out.println(""
+ lhm);
}
}
Output
{3=Geeks, 2=For, 1=Geeks}
2. Updating Elements in LinkedHashMap
To update an element in a LinkedHashMap, use the put() method again with the same key and a new value , it replaces the old value while keeping the insertion order.
import java.util.*;
class Geeks{
public static void main(String args[]) {
// Initialization of a LinkedHashMap
// using Generics
LinkedHashMap<Integer, String> lhm
= new LinkedHashMap<Integer, String>();
// Inserting mappings into Map
// using put() method
lhm.put(3, "Geeks");
lhm.put(2, "Geeks");
lhm.put(1, "Geeks");
// Printing mappings to the console
System.out.println("" + lhm);
// Updating the value with key 2
lhm.put(2, "For");
// Printing the updated Map
System.out.println("Updated Map: " + lhm);
}
}
Output
{3=Geeks, 2=Geeks, 1=Geeks}
Updated Map: {3=Geeks, 2=For, 1=Geeks}
3. Removing Element in LinkedHashMap
We can remove elements from a LinkedHashMap using the remove() method by specifying the key of the element to be deleted.
import java.util.*;
class Geeks{
public static void main(String args[]) {
// Initialization of a LinkedHashMap
// using Generics
LinkedHashMap<Integer, String> lhm
= new LinkedHashMap<Integer, String>();
// Inserting the Elements
// using put() method
lhm.put(3, "Geeks");
lhm.put(2, "Geeks");
lhm.put(1, "Geeks");
lhm.put(4, "For");
// Printing the mappings to the console
System.out.println("" + lhm);
// Removing the mapping with Key 4
lhm.remove(4);
// Printing the updated map
System.out.println("" + lhm);
}
}
Output
{3=Geeks, 2=Geeks, 1=Geeks, 4=For}
{3=Geeks, 2=Geeks, 1=Geeks}
4. Iterating through the LinkedHashMap
We can iterate through a LinkedHashMap using a for-each loop over map.entrySet(), accessing each key and value with getKey() and getValue() methods.
import java.util.*;
class Geeks{
public static void main(String args[]) {
// Initialization of a LinkedHashMap
// using Generics
LinkedHashMap<Integer, String> lhm
= new LinkedHashMap<Integer, String>();
// Inserting elements into Map
// using put() method
lhm.put(3, "Geeks");
lhm.put(2, "For");
lhm.put(1, "Geeks");
// For-each loop for traversal over Map
for (Map.Entry<Integer, String> mapElement :
lhm.entrySet()) {
Integer k = mapElement.getKey();
// Finding the value
// using getValue() method
String v = mapElement.getValue();
// Printing the key-value pairs
System.out.println(k + " : " + v);
}
}
}
Output
3 : Geeks 2 : For 1 : Geeks
Methods of LinkedHashMap
Below are some commonly used methods of the LinkedHashMap class:
| Method | Description |
|---|---|
| put(K key, V value) | Adds or updates a key-value pair in the map. |
| get(Object key) | Returns the value associated with the specified key. |
| remove(Object key) | Removes the mapping for the specified key. |
| containsKey(Object key) | Checks if the map contains the specified key. |
| containsValue(Object value) | Checks if the map contains the specified value. |
| clear() | Removes all key-value pairs from the map. |
| size() | Returns the number of key-value pairs in the map. |
| isEmpty() | Checks if the map is empty. |
| keySet() | Returns a Set view of all keys in the map. |
| values() | Returns a Collection view of all values in the map. |
| entrySet() | Returns a Set view of all key-value mappings in the map. |
| getOrDefault(Object key, V defaultValue) | Returns the value for a key, or a default value if the key is not found. |