• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
  •  
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic