Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't Java Map extends Collection?
    primarykey
    data
    text
    <p>I was surprised by the fact that <code>Map&lt;?,?&gt;</code> is not a <code>Collection&lt;?&gt;</code>.</p> <p>I thought it'd make a LOT of sense if it was declared as such:</p> <pre><code>public interface Map&lt;K,V&gt; extends Collection&lt;Map.Entry&lt;K,V&gt;&gt; </code></pre> <p>After all, a <code>Map&lt;K,V&gt;</code> is a collection of <a href="http://java.sun.com/javase/6/docs/api/java/util/Map.Entry.html" rel="noreferrer"><code>Map.Entry&lt;K,V&gt;</code></a>, isn't it?</p> <p>So is there a good reason why it's not implemented as such?</p> <hr> <p>Thanks to Cletus for a most authoritative answer, but I'm still wondering why, if you can already view a <code>Map&lt;K,V&gt;</code> as <code>Set&lt;Map.Entries&lt;K,V&gt;&gt;</code> (via <code>entrySet()</code>), it doesn't just extend that interface instead.</p> <blockquote> <p>If a <code>Map</code> is a <code>Collection</code>, what are the elements? The only reasonable answer is "Key-value pairs"</p> </blockquote> <p>Exactly, <code>interface Map&lt;K,V&gt; extends Set&lt;Map.Entry&lt;K,V&gt;&gt;</code> would be great!</p> <blockquote> <p>but this provides a very limited (and not particularly useful) <code>Map</code> abstraction.</p> </blockquote> <p>But if that's the case then why is <code>entrySet</code> specified by the interface? It must be useful somehow (and I think it's easy to argue for that position!).</p> <blockquote> <p>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.</p> </blockquote> <p>I'm not saying that that's all there is to it to <code>Map</code>! It can and <em>should</em> keep all the other methods (except <code>entrySet</code>, which is redundant now)!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload