Java 集中的重复值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10826179/
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
Duplicate Value in Java Set
提问by JR Galia
Java Set can't have a duplicate value. This is my code:
Java Set 不能有重复值。这是我的代码:
Set<String> set = new HashSet<String>();
Collections.addAll(set, arr);
If array arr have elements having the same value, set will have duplicate string value, why is it happening?
如果数组 arr 具有相同值的元素,则 set 将具有重复的字符串值,为什么会发生这种情况?
Where did i miss? Should I iterate the array and use the add method?
我错过了哪里?我应该迭代数组并使用 add 方法吗?
=============================================================
================================================== ============
Sorry, the above code works. I made a mistake in array arr. It's a whitespace thing.
抱歉,上面的代码有效。我在数组 arr 中犯了一个错误。这是一个空白的东西。
回答by jahroy
When I run the following code, it demonstrates that the set contains no duplicates:
当我运行以下代码时,它表明该集合不包含重复项:
class FunkTest
{
public static void main (String [] args)
{
Set<String> theHash = new HashSet<String>();
String[] theArray = new String[] {
"funky",
"garbage",
"funky",
"stuff",
"things",
"item",
"funky",
"funky"
};
Collections.addAll(theHash, theArray);
for (String thisItem : theHash) {
System.out.println(thisItem);
}
}
}
Output:
输出:
stuff
funky
item
things
garbage
There must be something different about your strings.
你的字符串一定有什么不同。
回答by Synesso
You are mistaken. A Set
will never contain a duplicate. No, you do not need to iterate and use the add
method.
你误会了。ASet
永远不会包含重复项。不,您不需要迭代和使用该add
方法。
Go back and take another look. What value(s) are duplicated? What happens if you add the array to a List<String>
instead?
回去再看看。哪些值是重复的?如果将数组添加到 a 会发生什么List<String>
?
回答by Michael
Most likely the objects aren't actually equal, so they're not the same Object. If you look at the javadocs for java.util.HashSet.add(), you'll see that the comparison to determine if an object is already there uses .equals(). Make sure you're 2 strings aren't different in any way that'd make String.equals() return false.
很可能这些对象实际上并不相等,因此它们不是同一个 Object。如果您查看java.util.HashSet.add()的 javadoc ,您将看到确定对象是否已经存在的比较使用 .equals()。确保您的 2 个字符串没有任何不同,这会使 String.equals() 返回 false。
回答by Savvas Dalkitsis
I can't see how this is happening since you are using Strings which has implemented equals properly.
我看不出这是如何发生的,因为您使用的是正确实现 equals 的字符串。
The following code prints "1" for both arrays. Are you sure you are not making a mistake?
以下代码为两个数组打印“1”。你确定你没有犯错吗?
import java.util.Set;
import java.util.HashSet;
import java.util.Collections;
class Main
{
public static void main(String[] args)
{
String[] arr1 = new String[]{"one","one"};
Set<String> set1 = new HashSet<String>();
Collections.addAll(set1, arr1);
System.out.println(set1.size());
String[] arr2 = new String[]{"two",new String("two")};
Set<String> set2 = new HashSet<String>();
Collections.addAll(set2, arr2);
System.out.println(set2.size());
}
}