Java HashMap, LinkedHashMap, and TreeMap – Complete Guide with Examples
Learn Java Map implementations—HashMap, LinkedHashMap, and TreeMap—their differences, features, and how to store and manage key-value pairs efficiently in Java applications.
HashMap, LinkedHashMap, and TreeMap in Java – Complete Detailed Tutorial
The Map interface in Java stores key-value pairs, and its commonly used implementations are HashMap, LinkedHashMap, and TreeMap. Each has different ordering and performance characteristics.
1. HashMap
- Implements Map interface
- Stores key-value pairs
- Keys are unique, values can be duplicated
- No guaranteed order
- Allows one null key and multiple null values
- Backed by hash table, provides fast access
Common Methods:
| MethodDescription | |
| put(K key, V value) | Adds a key-value pair |
| get(Object key) | Retrieves value for a key |
| remove(Object key) | Removes key-value pair |
| containsKey(Object key) | Checks if key exists |
| containsValue(Object value) | Checks if value exists |
| size() | Returns number of entries |
Example – Using HashMap
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Apple");
map.put(2, "Banana");
map.put(3, "Orange");
map.put(2, "Grapes"); // key 2 updated
System.out.println("HashMap: " + map);
System.out.println("Value for key 2: " + map.get(2));
map.remove(3);
System.out.println("After removal: " + map);
}
}
Output (order may vary):
HashMap: {1=Apple, 2=Grapes, 3=Orange}
Value for key 2: Grapes
After removal: {1=Apple, 2=Grapes}
2. LinkedHashMap
- Extends HashMap, implements Map interface
- Maintains insertion order
- Allows one null key and multiple null values
- Slightly slower than HashMap due to ordering maintenance
Example – Using LinkedHashMap
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<Integer, String> map = new LinkedHashMap<>();
map.put(1, "Apple");
map.put(2, "Banana");
map.put(3, "Orange");
System.out.println("LinkedHashMap: " + map);
map.remove(2);
System.out.println("After removal: " + map);
}
}
Output:
LinkedHashMap: {1=Apple, 2=Banana, 3=Orange}
After removal: {1=Apple, 3=Orange}
Explanation:
- Preserves insertion order
- Useful when order matters
3. TreeMap
- Implements SortedMap interface
- Stores key-value pairs in ascending sorted order by key
- Does not allow null keys, allows multiple null values
- Backed by Red-Black tree
- Slower than HashMap and LinkedHashMap due to sorting overhead
Example – Using TreeMap
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Map<Integer, String> map = new TreeMap<>();
map.put(3, "Orange");
map.put(1, "Apple");
map.put(2, "Banana");
System.out.println("TreeMap: " + map);
System.out.println("First key: " + ((TreeMap<Integer, String>) map).firstKey());
System.out.println("Last key: " + ((TreeMap<Integer, String>) map).lastKey());
map.remove(2);
System.out.println("After removal: " + map);
}
}
Output:
TreeMap: {1=Apple, 2=Banana, 3=Orange}
First key: 1
Last key: 3
After removal: {1=Apple, 3=Orange}
4. HashMap vs LinkedHashMap vs TreeMap
| FeatureHashMapLinkedHashMapTreeMap | |||
| Order | Unordered | Insertion order | Sorted order (ascending) |
| Null Key | Allowed (1) | Allowed (1) | Not allowed |
| Null Value | Allowed | Allowed | Allowed |
| Performance | Fast (O(1)) | Slightly slower | Slower (O(log n)) |
| Implementation | Hash table | Hash table + linked list | Red-Black tree |
5. Key Points
- Choose HashMap for fast access without order
- Choose LinkedHashMap to maintain insertion order
- Choose TreeMap to maintain sorted key order
- All implement Map interface, store key-value pairs, and provide methods like put, get, remove
6. Summary
- HashMap: fast, unordered, allows one null key
- LinkedHashMap: maintains insertion order
- TreeMap: sorted order by keys, no null keys
- Essential for efficient key-value storage in Java applications