为什么Map不扩展Collection?
这是设计使然.我们觉得
映射不是集合,并且
集合不是映射.因此,它
地图扩展几乎没有意义
收藏界面(或副界面)
反之亦然).
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上(keySet,
entrySet和值).虽然在
原则上,可以将列表视为
映射到元素的Map索引,
具有讨厌的特性
从列表中删除一个元素
更改与每个关联的密钥
元素在已删除元素之前.
这就是为什么我们没有地图视图
列表上的操作.
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: I think the quote answers most of the questions. It's worth stressing the part about a collection of entries not being a particularly useful abstraction. For example:
Set<Map.Entry<String,String>>
将允许:
set.add(entry("hello", "world"));
set.add(entry("hello", "world 2");
(假设创建一个Map.Entry
实例的entry()
方法)
(assuming an entry()
method that creates a Map.Entry
instance)
Map
需要唯一键,因此会违反此键.或者,如果您在条目的Set
上加上唯一键,那么从一般意义上说,它实际上并不是Set
.这是Set
,有更多限制.
Map
s require unique keys so this would violate this. Or if you impose unique keys on a Set
of entries, it's not really a Set
in the general sense. It's a Set
with further restrictions.
可以说Map.Entry
的equals()
/hashCode()
关系纯粹是关键,但即使这样也有问题.更重要的是,它真的增加了任何价值吗?一旦开始研究极端情况,您可能会发现这种抽象方法崩溃了.
Arguably you could say the equals()
/hashCode()
relationship for Map.Entry
was purely on the key but even that has issues. More importantly, does it really add any value? You may find this abstraction breaks down once you start looking at the corner cases.
值得注意的是,HashSet
实际上是作为HashMap
实现的,而不是相反的.这纯粹是实现细节,但仍然很有趣.
It's worth noting that the HashSet
is actually implemented as a HashMap
, not the other way around. This is purely an implementation detail but is interesting nonetheless.
entrySet()
存在的主要原因是简化遍历,因此您不必遍历键,然后查找键.不要将它作为Map
应该是条目(imho)的Set
的表面证据.
The main reason for entrySet()
to exist is to simplify traversal so you don't have to traverse the keys and then do a lookup of the key. Don't take it as prima facie evidence that a Map
should be a Set
of entries (imho).
这篇关于Java Map为什么不扩展Collection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!