Map in Java: HashMap, LinkedHashMap, TreeMap
Introduction
In Java, the Map interface is part of the java.util package. It represents a collection of key-value pairs, where each key is unique. Common implementations of the Map interface include:
- HashMap
- LinkedHashMap
- TreeMap
1. HashMap
A HashMap is an unordered collection that uses a hash table for storage. It does not guarantee the order of keys or values.
Example: Using HashMap
import java.util.HashMap; public class HashMapExample { public static void main(String[] args) { // Create a HashMap HashMapmap = new HashMap<>(); // Add key-value pairs to the HashMap map.put(1, "Apple"); map.put(2, "Banana"); map.put(3, "Cherry"); // Display the HashMap System.out.println("HashMap: " + map); // Access a value by key System.out.println("Value for key 2: " + map.get(2)); // Remove a key-value pair map.remove(2); System.out.println("After removal: " + map); // Iterate over the HashMap System.out.println("Iterating through HashMap:"); for (Integer key : map.keySet()) { System.out.println("Key: " + key + ", Value: " + map.get(key)); } } }
2. LinkedHashMap
A LinkedHashMap is an ordered version of HashMap that maintains a linked list of the entries in the order they were inserted.
Example: Using LinkedHashMap
import java.util.LinkedHashMap; public class LinkedHashMapExample { public static void main(String[] args) { // Create a LinkedHashMap LinkedHashMapmap = new LinkedHashMap<>(); // Add key-value pairs to the LinkedHashMap map.put(1, "Dog"); map.put(2, "Cat"); map.put(3, "Rabbit"); // Display the LinkedHashMap System.out.println("LinkedHashMap: " + map); // Add a duplicate key with a new value map.put(2, "Horse"); System.out.println("After updating key 2: " + map); // Iterate over the LinkedHashMap System.out.println("Iterating through LinkedHashMap:"); for (Integer key : map.keySet()) { System.out.println("Key: " + key + ", Value: " + map.get(key)); } } }
3. TreeMap
A TreeMap is a NavigableMap implementation that uses a red-black tree for storage. It maintains keys in their natural order or a specified comparator's order.
Example: Using TreeMap
import java.util.TreeMap; public class TreeMapExample { public static void main(String[] args) { // Create a TreeMap TreeMapmap = new TreeMap<>(); // Add key-value pairs to the TreeMap map.put(3, "Orange"); map.put(1, "Apple"); map.put(2, "Banana"); // Display the TreeMap System.out.println("TreeMap: " + map); // Access a value by key System.out.println("Value for key 1: " + map.get(1)); // Iterate over the TreeMap System.out.println("Iterating through TreeMap:"); for (Integer key : map.keySet()) { System.out.println("Key: " + key + ", Value: " + map.get(key)); } } }
Differences Between HashMap, LinkedHashMap, and TreeMap
Feature | HashMap | LinkedHashMap | TreeMap |
---|---|---|---|
Ordering | No order | Maintains insertion order | Sorted order |
Performance | Fast (O(1) for get, put) | Fast (O(1) for get, put) | Slower (O(log n) for get, put) |
Null Keys/Values | Allows one null key and multiple null values | Allows one null key and multiple null values | Does not allow null keys but allows null values |
Conclusion
Each Map implementation has its own use case:
- HashMap: Use for fast, unordered key-value mapping.
- LinkedHashMap: Use when you need to maintain insertion order.
- TreeMap: Use when you need sorted keys.