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

参见英文答案 > 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));

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...