Introduction
In Java, the Map interface is widely used for storing key-value pairs. But when choosing a specific implementation—HashMap, LinkedHashMap, or TreeMap—it can be confusing because they all store data in a similar way. However, they differ in ordering, internal working, performance, and use cases. This article explains each map type in simple, natural language along with code examples so you can easily understand when to use each one.
1. HashMap in Java
HashMap is the most commonly used map implementation. It stores data in no particular order.
Key Features
Does not maintain any order of keys.
Allows one null key and multiple null values.
Fast performance for basic operations: O(1) average time.
Uses hashing internally to store data.
Example
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(3, "Apple");
map.put(1, "Mango");
map.put(2, "Banana");
System.out.println(map); // Output: Order is unpredictable
}
}
When to Use HashMap
When you do NOT care about key ordering.
When you need fast lookups, inserts, and deletions.
For most general-purpose applications.
2. LinkedHashMap in Java
LinkedHashMap maintains the insertion order of elements. It works like HashMap but keeps a doubly-linked list internally.
Key Features
Maintains insertion order.
Slightly slower than HashMap due to ordering overhead.
Supports one null key and multiple null values.
Useful when predictable ordering is needed.
Example
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
map.put(3, "Apple");
map.put(1, "Mango");
map.put(2, "Banana");
System.out.println(map); // Output: {3=Apple, 1=Mango, 2=Banana}
}
}
When to Use LinkedHashMap
3. TreeMap in Java
TreeMap stores keys in sorted (ascending) order. It uses a Red-Black Tree internally.
Key Features
Automatically sorts keys.
Does NOT allow null keys (throws NullPointerException).
Slower than HashMap and LinkedHashMap.
Provides consistent ordering.
Example
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<>();
map.put(3, "Apple");
map.put(1, "Mango");
map.put(2, "Banana");
System.out.println(map); // Output: {1=Mango, 2=Banana, 3=Apple}
}
}
When to Use TreeMap
When you need data in sorted order.
When working with ranges (e.g., headMap, tailMap, subMap).
When predictable sorting is essential.
Key Differences Between HashMap, LinkedHashMap, and TreeMap
1. Ordering
2. Performance
| Operation | HashMap | LinkedHashMap | TreeMap |
|---|
| Insert | O(1) | O(1) | O(log n) |
| Search | O(1) | O(1) | O(log n) |
| Delete | O(1) | O(1) | O(log n) |
3. Null Support
HashMap: Allows 1 null key, many null values
LinkedHashMap: Same as HashMap
TreeMap: Does NOT allow null keys
4. Internal Working
Real-World Examples
When to Use HashMap
Storing user profiles
Configuration settings
Fast lookups in APIs
When to Use LinkedHashMap
When to Use TreeMap
Example: Comparing All Three
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer, String> hashMap = new HashMap<>();
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
Map<Integer, String> treeMap = new TreeMap<>();
int[] keys = {3, 1, 2};
for (int key : keys) {
hashMap.put(key, "Value" + key);
linkedHashMap.put(key, "Value" + key);
treeMap.put(key, "Value" + key);
}
System.out.println("HashMap: " + hashMap);
System.out.println("LinkedHashMap: " + linkedHashMap);
System.out.println("TreeMap: " + treeMap);
}
}
Output
HashMap: Random order
LinkedHashMap: {3=Value3, 1=Value1, 2=Value2}
TreeMap: {1=Value1, 2=Value2, 3=Value3}
Conclusion
HashMap, LinkedHashMap, and TreeMap all implement the Map interface but behave very differently. HashMap offers the best performance without order. LinkedHashMap preserves insertion order, making it suitable for predictable iteration. TreeMap automatically sorts keys, making it ideal for ordered or range-based operations. By understanding these differences, you can choose the right map for your Java application and write efficient, readable, and optimized code.