I was surprised by the fact that Map<?,?>
is not a Collection<?>
.
I thought it'd make a LOT of sense if it was declared as such:
public interface Map<K,V> extends Collection<Map.Entry<K,V>>
After all, a Map<K,V>
is a collection of Map.Entry<K,V>
, isn't it?
So is there a good reason why it's not implemented as such?
Thanks to Cletus for a most authoritative answer, but I'm still wondering why, if you can already view a Map<K,V>
as Set<Map.Entries<K,V>>
(via entrySet()
), it doesn't just extend that interface instead.
If a
Map
is aCollection
, what are the elements? The only reasonable answer is "Key-value pairs"
Exactly, interface Map<K,V> extends Set<Map.Entry<K,V>>
would be great!
but this provides a very limited (and not particularly useful)
Map
abstraction.
But if that's the case then why is entrySet
specified by the interface? It must be useful somehow (and I think it's easy to argue for that position!).
You can't ask what value a given key maps to, nor can you delete the entry for a given key without knowing what value it maps to.
I'm not saying that that's all there is to it to Map
! It can and should keep all the other methods (except entrySet
, which is redundant now)!