Java问题中的Map接口

发布于 2024-10-10 04:39:40 字数 115 浏览 0 评论 0原文

我一直想知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

撞了怀 2024-10-17 04:39:40

集合保证给定的条目只能在其中存在一次。集合则不然。由于 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.

℡Ms空城旧梦 2024-10-17 04:39:40

这实际上并不是优缺点的问题,而是映射 代表这很重要。

映射中的键是唯一的

Map 是唯一的——也就是说,Map 中不会有重复的键。 集合确保不存在重复项的是 设置

因此,键由 keySet 方法。

映射中的值不一定是唯一的

另一方面,Map 的值 不必是唯一的。

例如,我们可以在映射中将一个带有键 "fruit" 的条目映射到值 "apple",并且还有另一个带有键 " 的条目computer” 映射到值 “apple”

map {
  key:"fruit"    -> value:"apple"
  key:"computer" -> value:"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 a Map. A Collection which assures that duplicates don't exist is a Set.

Therefore, the keys are returned as a Set by the keySet 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":

map {
  key:"fruit"    -> value:"apple"
  key:"computer" -> 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 a Map is to return a plain-old Collection 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 the Map.Entry object. Since this key-value pair is unique, it is returned as a Set by the entrySet method.

Further reading

小嗲 2024-10-17 04:39:40

地图在内部管理键集,因为键是唯一的值不是

返回此映射中包含的键的集合视图。该集合由地图支持,因此对地图的更改会反映在集合中,反之亦然。如果在对集合进行迭代时修改映射(除非通过迭代器自己的删除操作),则迭代的结果是不确定的。该集合支持元素删除,即通过 Iterator.remove、Set.remove、removeAll、retainAll 和clear 操作从映射中删除相应的映射。不支持add或addAll操作。

另请参阅

Map internally manages Set of keys because keys are unique values aren't

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Also See

命比纸薄 2024-10-17 04:39:40

Set 是一个不包含重复元素的Collection。尽可能返回Set的优点是它可以明确地保证唯一性。

正如其他人已经指出的那样,values() 无法返回 Set,因为值的集合可能包含重复项。

A Set is a Collection that contains no duplicate elements. The advantage of returning a Set 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.

翻身的咸鱼 2024-10-17 04:39:40

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-duplicated Collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文