Java – compare any similar values of two arrays

See English answers > most effective way to return common elements from two string arrays 6

Find the scheme #1:2 in these two arrays, and the result is correct

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

Situation #2: there is no matching value, so the result is false

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

Are there any built-in methods in Java, or are there any libraries that can handle this requirement?

I want to emphasize that I am looking for a Java library or any Java method that can be used out of the box

Collection. Contains is not an option because all values in both arrays should be the same to return true (if at least one value in the two arrays is similar, you need to return true)

Solution

You can use collections #disjoint,

boolean isNoCommonElements = Collections.disjoint(
                                        Arrays.asList(x),Arrays.asList(y));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>