加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 比较两个数组的任何相似的值

发布时间:2020-05-25 02:21:08 所属栏目:Java 来源:互联网
导读:参见英文答案 Most efficient way to return common elements from two string arrays6 我想比较两个数组,如果两个数组中至少有一个值. 在这两个数组中找到方案#1:2,结果是正确的. String[] x = {1,2,3};String[] y

参见英文答案 > Most efficient way to return common elements from two string arrays6
我想比较两个数组,如果两个数组中至少有一个值.

在这两个数组中找到方案#1:2,结果是正确的.

String[] x = {"1","2","3"};
String[] y = {"2","5","6"};

情况#2:没有匹配的值,所以结果为false.

String[] x = {"1","3"};
String[] y = {"4","6"};

Java中有任何内置方法,还是可以处理此要求的任何库?

我想强调,我正在寻找一个Java库或任何可以开箱即用的Java方法.

Collection.contains不是一个选项,因为两个数组中的所有值应该相同,以返回true. (如果两个数组中至少有一个值相似,则需要返回true)

解决方法

你可以使用 Collections#disjoint,

Returns true if the two specified collections have no elements in
common.

…

Note that it is permissible to pass the same collection in both parameters,in which case the method will return true if and only if the collection is empty.

boolean isNoCommonElements = Collections.disjoint(
                                        Arrays.asList(x),Arrays.asList(y));

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读