在 Java 中检查一组字符串中的包含情况
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4284688/
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
Checking containment in a Set of strings in Java
提问by athena
I have a Set of String[]. I want to check whether this Set contains another String[].
我有一组字符串[]。我想检查这个 Set 是否包含另一个 String[]。
Set<String[]> s = new HashSet<String[]>();
s.add(new String[] {"lucy", "simon"});
System.out.println(s.contains(new String[] {"lucy", "simon"}));
However, false is printed. My guess is this is because only the references are being compared and not the actual Strings. It seems, the only option I have is to create a class, say Phrase, and implement hashCode()
and equals()
(that use Arrays.hashCode(...)
).
但是,会打印 false。我的猜测是这是因为只比较引用而不是实际的字符串。看来,我唯一的选择是创建一个类,比如 Phrase,然后实现hashCode()
和equals()
(使用Arrays.hashCode(...)
)。
Is there any other way to achieve what I want?
有没有其他方法可以实现我想要的?
采纳答案by Ralph
Your guess is correct: arrays ([]) do not implement a deep equals method: they are equals if they are the same instance.
您的猜测是正确的:数组 ([]) 没有实现深度等于方法:如果它们是同一个实例,则它们是相等的。
The simplest solution would be: replacing String[]
by List<String>
最简单的解决方案是:替换String[]
为List<String>
An other way (but i do not recommend it) is to implement your own Set, which does not based on Object.equals
but on java.util.Arrays.equals(Object[]a, Object[]b)
另一种方式(但我不推荐)是实现你自己的 Set,它不基于Object.equals
但基于java.util.Arrays.equals(Object[]a, Object[]b)
回答by Sanjay T. Sharma
Convert that String[]
to List<String>
and it should work out pretty well.
将其转换String[]
为List<String>
,它应该可以很好地工作。
Set<List<String>> s = new HashSet<List<String>>();
s.add(Arrays.asList("lucy", "simon"));
System.out.println(s.contains(Arrays.asList("lucy", "simon")));
回答by Emil
Use Set<Set<String>>
or Set<List<String>>
instead of Set<String[]>
使用Set<Set<String>>
或Set<List<String>>
代替Set<String[]>
Code:
代码:
List<String> s1=Arrays.asList("1","2"),s2=Arrays.asList("1","2");
System.out.println(s1.equals(s2) + " "+s1.hashCode()+ " "+s2.hashCode());
Output:
输出:
true 2530 2530
回答by Przemek Kryger
Sounds like you've already answered your question. One option is as you already stated. The another would be to use Set>, since the API for equals(Object)says:
听起来你已经回答了你的问题。一种选择是你已经说过的。另一个是使用Set>,因为equals(Object)的 API说:
Compares the specified object with this collection for equality.
比较指定对象与此集合是否相等。
回答by laher
I'd just loop through and call Arrays.equals:
我只是循环并调用 Arrays.equals:
something like this:
像这样:
boolean contains(Set<String[]> s, String[] item) {
for(String[] toCompare: s) {
if(Arrays.equals(toCompare, item)) {
return true;
}
}
return false;
}
not sure if it's the fastest but it should do the job nicely
不确定它是否是最快的,但它应该可以很好地完成工作
回答by haylem
Can the elements of the String[] be in different orders and still make the whole array be considered equal to another array containing the same elements in another order? If yes, you'd indeed be better off implementing a container class and overriding equals and hashcode.
String[] 的元素可以按不同的顺序排列,并且仍然使整个数组被认为等于另一个包含相同元素的另一个数组吗?如果是,您确实最好实现一个容器类并覆盖 equals 和 hashcode。
if not, and if storing the internal elements as Lists instead of arrays is an acceptable alternative, then you could do this:
如果不是,并且如果将内部元素存储为列表而不是数组是可以接受的替代方案,那么您可以这样做:
package com.stackoverflow;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class StringContainment {
public static void main(final String[] args) {
final Set<String[]> s = new HashSet<String[]>();
final Set<List<String>> s2 = new HashSet<List<String>>();
s.add(new String[] {"lucy", "simon"});
s2.add(Arrays.asList(new String[] { "lucy", "simon" }));
System.out.println(s.contains(new String[] {"lucy", "simon"})); // false
System.out.println(s2.contains(Arrays.asList(new String[] {"lucy", "simon"}))); // true
}
}
The first check will return false, the second true. Might be easier that way if you can use lists.
第一次检查将返回 false,第二次检查将返回 true。如果您可以使用列表,那样可能会更容易。
If you can't, you could still use this as long as you don't need to do this comparison too often (it's definitely not a good idea performance-wise).
如果你不能,你仍然可以使用它,只要你不需要太频繁地进行这种比较(这绝对不是一个好主意)。