Java LinkedHashMap
LinkedHashMap is a class that implements the Map interface.
It stores data as key-value pairs like HashMap, but it additionally maintains a predictable iteration order.
By default, that order is the insertion order.
Inserted:
1 -> Java
2 -> Python
3 -> C++Iteration:
1 -> Java
2 -> Python
3 -> C++LinkedHashMap belongs to the java.util package.
import java.util.LinkedHashMap;Why do we use LinkedHashMap?
LinkedHashMap is useful when:
- We need key-value storage.
- Keys must be unique.
- We want predictable iteration order.
- We want to preserve insertion order while using Map functionality.
For example:
import java.util.LinkedHashMap;
import java.util.Map;
public class HelloWorld {
public static void main(String[] args) {
Map<Integer, String> map = new LinkedHashMap<>();
map.put(101, "Ashish");
map.put(102, "Rahul");
map.put(103, "Priya");
System.out.println(map);
}
}The iteration order follows the order in which entries were inserted.
Creating a LinkedHashMap
LinkedHashMap<Integer, String> map =
new LinkedHashMap<>();Or preferably:
Map<Integer, String> map =
new LinkedHashMap<>();Adding Elements
Use put().
map.put(1, "Java");
map.put(2, "Python");
map.put(3, "C++");Insertion Order
Suppose we insert:
map.put(3, "C");
map.put(1, "Java");
map.put(2, "Python");When iterating, the entries are encountered in insertion order:
3 -> C
1 -> Java
2 -> PythonThe keys are not automatically sorted.
This is an important difference between LinkedHashMap and TreeMap.
Updating an Existing Key
If an existing key is added again:
map.put(2, "Python Programming");Its value is updated.
The entry does not become a newly inserted key in the normal insertion-order mode.
Duplicate Keys
Duplicate keys are not allowed.
map.put(1, "Java");
map.put(1, "Python");Final mapping:
1 -> PythonDuplicate Values
Duplicate values are allowed.
map.put(1, "Java");
map.put(2, "Java");This is valid.
Null Keys and Values
LinkedHashMap permits:
- One null key.
- Multiple null values.
Example
map.put(null, "Unknown");
map.put(1, null);
map.put(2, null);Iterating Through LinkedHashMap
for (Map.Entry<Integer, String> entry :
map.entrySet()) {
System.out.println(
entry.getKey() + " -> " + entry.getValue()
);
}The entries are encountered in insertion order.
Access-Order Mode
One useful feature of LinkedHashMap is that it can also maintain access order.
This can be enabled using its constructor:
LinkedHashMap<Integer, String> map =
new LinkedHashMap<>(16, 0.75f, true);The third parameter:
trueenables access-order mode.
In this mode, accessing an existing entry can move that entry toward the end of the iteration order.
For example:
map.put(1, "Java");
map.put(2, "Python");
map.put(3, "C++");
map.get(1);After accessing key 1, the iteration order can become:
2 -> Python
3 -> C++
1 -> JavaThis behavior makes LinkedHashMap useful in certain cache-related designs.
Example: Maintaining Order
Suppose an application receives events:
Map<Integer, String> events =
new LinkedHashMap<>();
events.put(1, "Login");
events.put(2, "Search");
events.put(3, "Logout");When iterating:
1 -> Login
2 -> Search
3 -> LogoutThe insertion sequence is preserved.
Important LinkedHashMap Methods
Because LinkedHashMap implements Map, it provides common Map methods such as:
| Method | Purpose |
|---|---|
| put() | Adds or updates an entry |
| get() | Retrieves a value |
| remove() | Removes an entry |
| containsKey() | Checks for a key |
| containsValue() | Checks for a value |
| size() | Returns number of entries |
| isEmpty() | Checks whether map is empty |
| clear() | Removes all entries |
| keySet() | Returns keys |
| values() | Returns values |
| entrySet() | Returns key-value entries |
LinkedHashMap vs HashMap
| LinkedHashMap | HashMap |
|---|---|
| Maintains predictable iteration order | No guaranteed iteration order |
| Usually maintains insertion order | Order should not be relied upon |
| Has additional ordering-related overhead | Simpler internal structure |
| Useful when order matters | Useful when order does not matter |
LinkedHashMap vs TreeMap
| LinkedHashMap | TreeMap |
|---|---|
| Maintains insertion/access order | Maintains sorted key order |
| Does not sort keys | Sorts keys |
| Hash-based | Tree-based |
| Basic operations are typically O(1) average | Basic operations are O(log n) |
Is LinkedHashMap Thread-Safe?
LinkedHashMap is not synchronized.
If multiple threads access and modify the same map concurrently, appropriate synchronization or a concurrent collection may be required.
Important Points
- LinkedHashMap implements Map.
- It stores key-value pairs.
- Keys are unique.
- Duplicate values are allowed.
- One null key is allowed.
- Multiple null values are allowed.
- It maintains predictable iteration order.
By default, iteration follows insertion order. It can also be configured for access-order behavior. It is not synchronized.
Key Takeaway
Use LinkedHashMap when you need a Map and the order in which entries are iterated matters.
Map<Integer, String> map =
new LinkedHashMap<>();It is especially useful when you want the key-value behavior of a Map while preserving a predictable order.
See you in next page....🙂