Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Map doesn't extend collection interface while List and Set can extend Collection interface

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Is there any reason Why Map doesn't extend collection interface while List and Set can extend Collection interface ?
 
Paul Clapham
Sheriff
Pie
Posts: 20763
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. A Map simply isn't a collection so there is no reason why it should implement Collection.

Also, you could ask yourself how the Map<K, V> interface could possibly implement the Collection<E> interface. When you see it stated in generic terms it's easier to see that it doesn't make sense.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Yes. A Map simply isn't a collection so there is no reason why it should implement Collection.

Also, you could ask yourself how the Map<K, V> interface could possibly implement the Collection<E> interface. When you see it stated in generic terms it's easier to see that it doesn't make sense.
hi Paul, There should be a reason for each and everything. Without reason, nothing will exist/happen. could you please provide more information on "Also, you could ask yourself how the Map<K, V> interface could possibly implement the Collection<E> interface. When you see it stated in generic terms it's easier to see that it doesn't make sense." I am not clear
 
Paul Clapham
Sheriff
Pie
Posts: 20763
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you were going to make Map extend Collection what would you do?

... so that a Map is a collection of its keys... or

... so that a Map is a collection of its values... or something else?
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harikrishna,

It's true that Map has set of keys and collection of values in it.
So, it will be meaningless for Map to inherit Collection, which has only collection of values.

Collection<E> has methods like:
  • boolean add(E Element)
  • boolean addAll(Collection<? extends E> C)


  • won't mean much in Map, which has set of keys in addition.
    That is why, Map<K, V> has second parameter in analogous functions, as follows:
  • Object put(K key, V value)
  • void putAll(Map <? extends K, ? extends V> map)
  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic