The Map Interface
The Map interface is not a subtype of the Collection interface. Therefore, its behavior is slightly different from the others.
A Map represents a mapping between a Key and a Value.
- A Map cannot contain duplicate keys (each key can map to at most one value).
- It is perfectly fine to have duplicate values, as long as they are assigned to different keys.
1. HashMap
The HashMap class is the default, go-to implementation of the Map interface. It stores the data in (Key, Value) pairs using a hash table.
Key Characteristics
- No Predictable Order: Like
HashSet, it makes no guarantees about the order of the map. - Fast Performance: Provides constant-time performance $O(1)$ for basic operations (
getandput). - Nulls Allowed: Allows one
nullkey and multiplenullvalues.
Example
import java.util.HashMap;
import java.util.Map;
public class LabMap1 {
public static void main(String args[]) {
// Creating a Map with String Keys and Integer Values
Map<String, Integer> scores = new HashMap<>();
// Adding Key-Value pairs
scores.put("Alice", 95);
scores.put("Bob", 82);
scores.put("Charlie", 95); // Duplicate values are allowed!
// Overwriting a value (since keys must be unique)
scores.put("Bob", 88);
// Retrieving a value
System.out.println("Bob's score is: " + scores.get("Bob")); // 88
// Iterating through the Map
for (Map.Entry<String, Integer> entry : scores.entrySet()) {
System.out.println(
"Key: " + entry.getKey() + ", Value: " + entry.getValue()
);
}
}
}
2. LinkedHashMap
The LinkedHashMap class extends HashMap. It maintains a doubly-linked list running through all of its entries.
Key Characteristics
- Predictable Iteration Order: Unlike
HashMap, it maintains insertion order. When you iterate through it, elements will be returned in the exact order they were inserted. - Slightly Slower: Marginal performance hit due to maintaining the linked list.
import java.util.LinkedHashMap;
import java.util.Map;
public class LabMap2 {
public static void main(String args[]) {
Map<Integer, String> lmap = new LinkedHashMap<>();
lmap.put(100, "Amit");
lmap.put(101, "Vijay");
lmap.put(102, "Rahul");
// Will print exactly in the inserted order
System.out.println(lmap);
}
}
3. TreeMap
The TreeMap class implements the SortedMap interface (which extends Map). It uses a Red-Black tree structure.
Key Characteristics
- Sorted Keys: It stores elements sorted according to the natural ordering of its keys, or by a
Comparatorprovided at map creation time. - Slower Performance: Basic operations (
getandput) take $O(\log n)$ time. - No Null Keys: It does not allow
nullkeys.
4. Hashtable (Legacy)
The Hashtable class is very similar to HashMap, but with two major differences:
- Synchronized: It is thread-safe, meaning multiple threads cannot modify it simultaneously. This makes it slower than
HashMap. - No Nulls: It does not allow any
nullkey ornullvalue.
Because of the performance overhead of synchronization, modern Java development prefers using ConcurrentHashMap for thread-safe map operations instead of the legacy Hashtable.