在 Java 中检查字符串集中的包含情况
我有一组 String[]。我想检查这个 Set 是否包含另一个 String[]。
Set<String[]> s = new HashSet<String[]>();
s.add(new String[] {"lucy", "simon"});
System.out.println(s.contains(new String[] {"lucy", "simon"}));
然而,打印的是 false。我的猜测是这是因为只比较引用而不是实际的字符串。看来,我唯一的选择就是创建一个类,比如 Phrase,并实现 hashCode()
和 equals()
(使用 Arrays.hashCode( ...)
)。
还有其他方法可以实现我想要的吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您的猜测是正确的:数组 ([]) 没有实现深度 equals 方法:如果它们是同一实例,则它们相等。
最简单的解决方案是:用
List
替换String[]
另一种方法(但我不推荐它)是实现你自己的 Set,它不会基于
Object.equals
但基于java.util.Arrays.equals(Object[]a, Object[]b)
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[]
byList<String>
An other way (but i do not recommend it) is to implement your own Set, which does not based on
Object.equals
but onjava.util.Arrays.equals(Object[]a, Object[]b)
将
String[]
转换为List
,效果应该很好。Convert that
String[]
toList<String>
and it should work out pretty well.String[] 的元素是否可以采用不同的顺序,并且仍然使整个数组被视为等于另一个包含不同顺序的相同元素的数组?如果是,那么您确实最好实现一个容器类并覆盖 equals 和 hashcode。
如果不是,并且如果将内部元素存储为列表而不是数组是可接受的替代方案,那么您可以这样做:
第一次检查将返回 false,第二次检查将返回 true。
如果您可以使用列表,这种方式可能会更容易。
如果你不能,你仍然可以使用它,只要你不需要太频繁地进行这种比较(从性能角度来说,这绝对不是一个好主意)。
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.
if not, and if storing the internal elements as Lists instead of arrays is an acceptable alternative, then you could do this:
The first check will return false, the second true.
Might be easier that way if you can use lists.
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).
听起来你已经回答了你的问题。一种选择正如您已经说过的那样。另一种方法是使用Set>,因为equals(Object)的API说:
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>
或Set
而不是>
Set
代码:
输出:
Use
Set<Set<String>>
orSet<List<String>>
instead ofSet<String[]>
Code:
Output:
我只是循环并调用 Arrays.equals:
像这样的东西:
不确定它是否是最快的,但它应该很好地完成工作
I'd just loop through and call Arrays.equals:
something like this:
not sure if it's the fastest but it should do the job nicely
以Java8流介绍您可以通过以下方式进行操作:
With Java8 stream introduction you can do it the following way: