| Author |
Why Map doesn't extend collection interface while List and Set can extend Collection interface
|
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
|
Hi, Is there any reason Why Map doesn't extend collection interface while List and Set can extend Collection interface ?
|
OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
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
Joined: Sep 23, 2010
Posts: 422
|
|
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
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
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
Joined: Sep 26, 2010
Posts: 155
|
|
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)
|
OCPJP6
|
 |
 |
|
|
subject: Why Map doesn't extend collection interface while List and Set can extend Collection interface
|
|
|