Why is Map
not a Collection
, while List
and Set
are?
The reason Map<K, V>
is not a subtype of Collection<E>
is because it does not store individual elements, unlike List<E>
and Set<E>
. Instead, it stores key-value pairs, which have a fundamentally different structure and behavior.
1. List
and Set
Extend Collection
, but Map
Does Not
Collection<E>
is designed for storing and managing individual elements.List<E>
andSet<E>
are both groups of elements, so they extendCollection<E>
.Map<K, V>
stores key-value pairs, not single elements, so it does not fit into theCollection<E>
model.
Hierarchy Comparison
Collection<E>
├── List<E> (Ordered, allows duplicates)
│ ├── ArrayList<E>
│ ├── LinkedList<E>
├── Set<E> (No duplicates)
│ ├── HashSet<E>
│ ├── TreeSet<E>
Map<K, V> (Separate hierarchy)
├── HashMap<K, V>
├── LinkedHashMap<K, V>
├── TreeMap<K, V>
👉 Map<K, V>
does not extend Collection<E>
because it does not store single elements—instead, it stores key-value pairs.
2. Fundamental Differences Between Collection<E>
and Map<K, V>
Feature | Collection<E> (List, Set) | Map<K, V> |
---|---|---|
Stores | Individual elements (E ) | Key-value pairs (K, V ) |
Duplicate Keys? | N/A (No keys) | ❌ No (Keys must be unique) |
Duplicate Values? | ✅ List allows duplicates, ❌ Set does not | ✅ Yes (Values can be duplicate) |
Access method | add(E e) , remove(E e) , contains(E e) | put(K, V) , get(K) , containsKey(K) |
Iteration | Iterates over elements (for-each loop ) | Iterates over keys, values, or key-value pairs (entrySet() ) |
3. Example: List
vs. Set
vs. Map
List<E>
Example (Extends Collection
)
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Alice");
list.add("Bob");
list.add("Alice"); // Duplicates allowed
System.out.println(list); // Output: [Alice, Bob, Alice]
}
}
✅ List
behaves like a collection of individual elements.
Set<E>
Example (Extends Collection
)
import java.util.*;
public class Main {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Alice");
set.add("Bob");
set.add("Alice"); // Ignored (No duplicates allowed)
System.out.println(set); // Output: [Alice, Bob]
}
}
✅ Set
is still a collection of single elements but removes duplicates.
Map<K, V>
Example (Does NOT Extend Collection
)
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Alice", 25);
map.put("Bob", 30);
map.put("Alice", 28); // Overwrites existing value for "Alice"
System.out.println(map); // Output: {Alice=28, Bob=30}
}
}
- ✅
Map
stores key-value pairs. - ❌ Not a
Collection<E>
, because it doesn’t hold just elements.
4. How to Get a Collection
View from a Map
Even though Map<K, V>
is not a Collection<E>
, we can extract views that behave like collections:
map.keySet()
→ Returns aSet<K>
of keys.map.values()
→ Returns aCollection<V>
of values.map.entrySet()
→ Returns aSet<Map.Entry<K, V>>
of key-value pairs.
Example: Extracting Collections from a Map
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Alice", 25);
map.put("Bob", 30);
// Get collections from the map
Set<String> keys = map.keySet(); // {Alice, Bob}
Collection<Integer> values = map.values(); // {25, 30}
Set<Map.Entry<String, Integer>> entries = map.entrySet(); // {Alice=25, Bob=30}
System.out.println("Keys: " + keys);
System.out.println("Values: " + values);
System.out.println("Entries: " + entries);
}
}
5. Final Answer
List<E>
andSet<E>
areCollection<E>
because they store individual elements.Map<K, V>
is NOT aCollection<E>
because it stores key-value pairs instead of individual elements.- However,
Map<K, V>
is part of the Java Collections Framework and provides methods to retrieve collection-like views (keySet()
,values()
,entrySet()
).