Blog / April 23, 2021 / 2 mins read / By Suneet Agrawal

Why Map does not extend Collection interface?

Collections in any language is an interface that stores similar data type objects and provides an iteration functionality. The common extensions of Collection are List and Set.

The Map is a well-known data structure used to store key-value pairs where keys will be unique.

We can take a reference of any language but let’s look at the example of Java for reference as Collection Framework from Java is very popular.

Java has Iterable interface which is extended by Collection. The Collection is further extended by List, Queue and Set which has their different-different implementations but the unique thing notice is that the Map interface doesn’t extend Collection interface.

Why Map does not extend Collection interface

Why Map does not extend Collection interface?

  • Because they are of an incompatible type.
    List, Set and Queue are a collection of similar kind of objects but just values where a Map is a collection of key and value pairs.
  • List Set and Queue have add as a function which takes a value as param to add an element whereas Map has put as a function which takes a key and a value as params to add a key-value pair.
  • List, Set and Queue provide iterate functionality over the value whereas Maps has keys to iterate over which is ultimately a Set and Values as Collection.
public interface Map<K,V> {
    ...

    Set<K>; keySet();

    Collection<V> values();

    ...
}
Comments