0

I was reading Java Collection Framework at its official website.
There is one point - "A Map is not a true Collection"
What does it mean ? Why map is not a part of Collection ?
Then I came to know Map does not extends Collection. What is the purpose of doing this ? It would be better if Map is a part of Collection, isn't it ?
Please refer below hierarchy for Collection framework :-
enter image description here

unknown
  • 4,859
  • 10
  • 44
  • 62

1 Answers1

3

A Map cannot be a Collection because their semantics are at odds. The closest thing to "collectionness" for a Map is when it is viewed as an Set<Map.Entry<K,V>>. You can get that as a collection view of the map by calling map.entrySet(); however it would cause more trouble than good if the Map itself implemented the complete Collection API. For example, the method remove(Object) is defined in both Collection and Map, but with incompatible semantics. The names of such methods would have to be warped, affecting all Collections, just to fit the awkward case of Map.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436