JAVA 集合,无重复

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23543663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:45:48  来源:igfitidea点击:

JAVA collections, no duplicates

javacollections

提问by nenito

I want to discuss a question about java collection framework. Here is the question:

我想讨论一个关于java集合框架的问题。这是问题:

You need to store elements in a collection that guarantees that no duplicates are stored. Which one of the following interfaces provide that capability?

您需要将元素存储在保证不存储重复项的集合中。以下哪个接口提供了该功能?

a.java.util.List

a.java.util.List

b.java.util.Collection

b.java.util.Collection

c.java.util.Map

c.java.util.Map

d.none of the above

d.以上都不是

It is pretty clear that the first two options are incorrect, but which one is true c. or d. and why? Personally, my answer is d.none of the above.

很明显,前两个选项是不正确的,但哪个是正确的 c. 或 d。为什么?就我个人而言,我的回答是 d.none 以上。

采纳答案by anirudh

Map does not allow duplicate keys of course, but allows duplicate values. So I think the answer would be d). The collection that does not allow any duplicates is Set.

Map 当然不允许重复的键,但允许重复的值。所以我认为答案是d)。不允许任何重复的集合是Set.

回答by bigGuy

Interfaces provide no capabilities at all. So, d)

接口根本不提供任何功能。所以,d)

回答by Absurd-Mind

The answer should be C:

答案应该是C:

Take a look at the Map javadoc

看一下Map javadoc

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

将键映射到值的对象。地图不能包含重复的键;每个键最多可以映射到一个值。

so with something like this you can achieve the no duplicates requirement (Which is basically the implementation of HashSet):

所以用这样的东西你可以实现无重复的要求(这基本上是 HashSet 的实现):

String testString = "foo";
Map<String, String> map;
map.put(testString, testString);

回答by Harshal Patil

A java.util.Mapcannot contain duplicate keys.

Ajava.util.Map不能包含重复的键。

Refer this Documetation

请参阅此文档

Get more details with example on this Example

通过此示例的示例获取更多详细信息

回答by Shailendra Singh

Set does not allow duplicates, Map will have unique key but can have duplicate values.

Set 不允许重复,Map 将具有唯一键但可以具有重复值。