Java.Collections.

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> and Set<E> are both groups of elements, so they extend Collection<E>.
  • Map<K, V> stores key-value pairs, not single elements, so it does not fit into the Collection<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>

FeatureCollection<E> (List, Set)Map<K, V>
StoresIndividual 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 methodadd(E e), remove(E e), contains(E e)put(K, V), get(K), containsKey(K)
IterationIterates 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 a Set<K> of keys.
  • map.values() → Returns a Collection<V> of values.
  • map.entrySet() → Returns a Set<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> and Set<E> are Collection<E> because they store individual elements.
  • Map<K, V> is NOT a Collection<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()).
This entry was posted in Без рубрики. Bookmark the permalink.