Java问题中的Map接口
我一直想知道 java.util 中的 Map。
为什么values()方法返回一个Collection,而keySet和entrySet返回一个Set?
套装和收藏有哪些优点/缺点?
I have been wondering about the Map from java.util.
Why does values() method return a Collection while the keySet and entrySet return a Set?
What's the advantages/disadvantages of a Set and Collection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
集合保证给定的条目只能在其中存在一次。集合则不然。由于 Map 在值方面没有唯一性保证,因此它们的集合根本不是真正的集合,而必须是集合。
A set guarantees that a given entry can only exist in it once. A Collection doesn't. Since a Map has no uniqueness guarantees in terms of values, the set of them isn't really a set at all but would have to be a Collection.
这实际上并不是优缺点的问题,而是映射 代表这很重要。
映射中的键是唯一的
Map
是唯一的——也就是说,Map
中不会有重复的键。集合
确保不存在重复项的是设置
。因此,键由
keySet
方法。映射中的值不一定是唯一的
另一方面,
Map
的值 不必是唯一的。例如,我们可以在映射中将一个带有键
"fruit"
的条目映射到值"apple"
,并且还有另一个带有键" 的条目computer”
映射到值“apple”
:允许在映射中存在重复值。
因此,我们不能使用
Set
,因为这需要所有条目都是唯一的。对于Map
的值来说,一个不错的选择是返回一个普通的Collection
,因为它不会对值强加任何限制。映射中的条目也是唯一的
Map
的条目是唯一的——它们是键和值的组合,由Map.Entry
对象。由于此键值对是唯一的,因此它由entrySet
方法。进一步阅读
It's not really an issue of advantages and disadvantages -- it's what the keys, values and entries of a map represent that's important.
Keys in a map are unique
The keys in a
Map
are unique -- that is, there aren't going to be duplicate keys in aMap
. ACollection
which assures that duplicates don't exist is aSet
.Therefore, the keys are returned as a
Set
by thekeySet
method.Values in a map are not necessarily unique
On the other hand, the values of a
Map
does not have to be unique.For example, we could have an entry in a map with the key
"fruit"
map to the value"apple"
, and also have another entry with key"computer"
mapping to the value"apple"
:Having duplicate values in a map is allowed.
Therefore, we cannot use a
Set
, as that necessitates that all the entries unique. A good choice for the values of aMap
is to return a plain-oldCollection
as it does not impose any restrictions to what the values are.Entries in a map are also unique
The entries of the
Map
are unique -- they are a combination of the key and value, represented by theMap.Entry
object. Since this key-value pair is unique, it is returned as aSet
by theentrySet
method.Further reading
地图在内部管理键集,因为键是唯一的值不是
另请参阅
Map internally manages Set of keys because keys are unique values aren't
Also See
Set
是一个不包含重复元素的Collection
。尽可能返回Set
的优点是它可以明确地保证唯一性。正如其他人已经指出的那样,values() 无法返回 Set,因为值的集合可能包含重复项。
A
Set
is aCollection
that contains no duplicate elements. The advantage of returning aSet
when possible is that it makes the guarantee of uniqueness explicit.As already pointed out by others,
values()
cannot return a Set because the collection of values may contain duplicates.value() 可以重复,因此它是
集合
。keySet() 和entrySet() 不能重复,因此它们是
Set
。ps:
Set
是一个不重复的Collection
。values() could be duplicated, so it is
Collection
.keySet() and entrySet() couldn't be duplicated, so they are
Set
.ps:
Set
is a non-duplicatedCollection
.