http://docs.oracle.com/javase/8/docs/technotes/guides/collections/ Designfaq.html#a14” rel=“noreferer”
Why doesn t Map extend Collection?
This was by design. We feel that
mappings are not collections and
collections are not mappings. Thus, it
makes little sense for Map to extend
the Collection interface (or vice
versa).
If a Map is a Collection, what are the
elements? The only reasonable answer
is "Key-value pairs", but this
provides a very limited (and not
particularly useful) Map abstraction.
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.
Collection could be made to extend
Map, but this raises the question:
what are the keys? There s no really
satisfactory answer, and forcing one
leads to an unnatural interface.
Maps can be viewed as Collections (of
keys, values, or pairs), and this fact
is reflected in the three "Collection
view operations" on Maps (keySet,
entrySet, and values). While it is, in
principle, possible to view a List as
a Map mapping indices to elements,
this has the nasty property that
deleting an element from the List
changes the Key associated with every
element before the deleted element.
That s why we don t have a map view
operation on Lists.
<>Update: 我认为,大多数问题都得到了答案。 值得强调的是,收集条目并非特别有用的摘要。 例如:
Set<Map.Entry<String,String>>
允许:
set.add(entry("hello", "world"));
set.add(entry("hello", "world 2"));
(假设一种<代码> 输入()方法,该方法可生成Map.Entry
.
<代码>Map要求有独特的钥匙,从而违反此规定。 或者如果你在<代码>上规定独特的钥匙 条目代码>从一般意义上说不是<代码>Set。 它有进一步限制的<条码>Set。
可以说,<代码>等值(/hashCode(>
relationship for Map.Entry
纯属关键,但甚至有问题。 更重要的是,这是否真正增加了任何价值? 你们一旦开始研究独角兽案件,就可能会发现这种抽象现象。
值得注意的是,<代码>HashSet实际上是作为而不是以其他方式实施的。 这纯粹是一个执行细节,但令人感兴趣。
之所以存在<代码>enterSet()的主要原因是要简化手续,这样你就不必绕过钥匙,然后对钥匙进行调查。 Don t视之为初步证据:Map
应为的条目(imho)。