A Map in Java is a data structure that stores elements in key–value pairs. Each key is unique, but values may be duplicate.
Defined in:
java.util.MapMaps are not part of the Collection hierarchy (List, Set, Queue), but they are a critical part of the Collections Framework.
| Feature | Description |
|---|---|
| Stores | Key → Value pairs |
| Duplicate keys | ❌ Not allowed |
| Duplicate values | ✔ Allowed |
| Null keys | Depends on implementation |
| Null values | Depends on implementation |
| Ordering | Depends on implementation |
put(K key, V value)
get(Object key)
remove(Object key)
containsKey(Object key)
containsValue(Object value)
keySet()
values()
entrySet()
size()
clear()
isEmpty()Example:
Map<Integer, String> map = new HashMap<>();
map.put(1, "A");
map.put(2, "B");
System.out.println(map.get(1)); // AJava provides several Map types with different behaviors.
- No ordering
- Allows 1 null key, multiple null values
- Fast (O(1) average for get/put)
- Not synchronized (not thread-safe)
Map<Integer, String> map = new HashMap<>();
map.put(101, "Java");
map.put(102, "Python");- Keeps elements in insertion order
- Slightly slower than HashMap
- Allows one null key
Map<String, Integer> map = new LinkedHashMap<>();
map.put("A", 1);
map.put("B", 2);Output:
{A=1, B=2}
- Stores keys in sorted order (ascending)
- No null key allowed
- Based on Red-Black Tree
- Slower (O(log n))
Map<Integer, String> map = new TreeMap<>();
map.put(30, "C");
map.put(10, "A");
map.put(20, "B");Output:
{10=A, 20=B, 30=C}
- Thread-safe (synchronized)
- No null keys / null values allowed
- Slower than HashMap
Map<Integer, String> map = new Hashtable<>();Mostly replaced by ConcurrentHashMap.
- Modern replacement for Hashtable
- Thread-safe and high performance
- No null key/value allowed
- Used in multithreading & backend systems
Map<String, Integer> map = new ConcurrentHashMap<>();| Feature | HashMap | LinkedHashMap | TreeMap | Hashtable | ConcurrentHashMap |
|---|---|---|---|---|---|
| Order | None | Insertion | Sorted | None | None |
| Null Key | Yes | Yes | No | No | No |
| Null Value | Yes | Yes | Yes | No | No |
| Thread Safe? | No | No | No | Yes | Yes |
| Speed | Fastest | Medium | Slow | Slow | Fast under load |
| Use Case | General | Ordered iteration | Sorted data | Legacy code | High concurrency |
for (Integer key : map.keySet()) {
System.out.println(key + " => " + map.get(key));
}for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}map.forEach((k, v) -> System.out.println(k + " -> " + v));Returns all keys:
Set<K> keys = map.keySet();Returns all values:
Collection<V> values = map.values();Returns all key-value pairs:
Set<Map.Entry<K, V>> entries = map.entrySet();-
HashMap stores entries in buckets using an array of nodes
-
hashCode()determines the bucket index -
Collision resolution:
- Linked list (Java 7)
- Linked list → Red-Black Tree (Java 8, threshold = 8)
-
equals()used to compare keys -
Rehashing happens when size > capacity * load factor (default = 0.75)
- Uses TreeMap.Entry nodes
- Balanced using Red-Black Tree
- Keys must be comparable
- No null keys allowed
- Maintains sorted order
| Scenario | Best Map |
|---|---|
| General-purpose | HashMap |
| Maintain insertion order | LinkedHashMap |
| Need sorted map | TreeMap |
| Thread-safe | ConcurrentHashMap |
| Legacy systems | Hashtable |
Map<String, Integer> freq = new HashMap<>();
for (String word : words) {
freq.put(word, freq.getOrDefault(word, 0) + 1);
}Map<Integer, String> students = new HashMap<>();
students.put(101, "John");
students.put(102, "Sarah");LinkedHashMap cache = new LinkedHashMap(16, 0.75f, true) {
protected boolean removeEldestEntry(Map.Entry e) {
return size() > 5;
}
};Because Map deals with pairs, not individual elements.
Because all null keys would hash to the same bucket.
Using linked lists, and balanced trees after Java 8.
Because null cannot be compared during sorting.
- HashMap not synchronized
- Hashtable synchronized and legacy
-
Map stores key–value pairs; keys must be unique.
-
HashMap → fastest & most commonly used.
-
LinkedHashMap → maintains insertion order.
-
TreeMap → sorted map.
-
Hashtable → legacy, synchronized.
-
ConcurrentHashMap → modern thread-safe map.
-
Use
entrySet()for efficient iteration. -
Internal workings rely heavily on hashing, comparison, and trees.