0

I am fairly new to java streams and wonder whether there is an easy solution using java streams to check if any element in array 1 is also present in array 2

example:

array1 = ["banana","apple","cat"]
array2 = ["toast","bread","pizza","banana"]
--> return true

array1 = ["banana","apple","cat"]
array2 = ["toast","bread","pizza"]
--> return false

Thanks!

1
  • 3
    Have you tried anything with code that you can show us? Commented Jul 12, 2019 at 4:56

2 Answers 2

4

Just use Collections.disjoint. This method checks if any elements of the both arrays are common.

Collections.disjoint(Arrays.asList(array1), Arrays.asList(array2))
Sign up to request clarification or add additional context in comments.

12 Comments

1+; what I really don't like about disjoint is that the method parameters are declared using wildcards, as such I can call it with Collections.disjoint(List.of(""), List.of(1)); - though Integer and String are completely un-related, this will still compile.
@Eugene Note, though, that disjoint is allowed to throw a ClassCastException “if one collection contains an element that is of a type which is ineligible for the other collection.” (on my Java 9 it didn’t in your example).
@OleV.V. to be honest I am a bit confused now, because I though that if I declare a method like static <T, U extends T> boolean disjoint(List<T> left, List<U> right) { return false; } and use it as : disjoint(List.of(""), List.of(1)); this would not compile, but to my surprise, it does. One the other hand List<String> strings = List.of(""); disjoint(strings, List.of(1)); this does not. it seems like the compiler infers List.of("") to a type that we can't normally create
Curious, right, though we’re digressing. In the former example my Eclipse infers List<Serializable> for List.of("") and List<Integer> for List.of(1), so that compiles. In the latter example you specify List<String>, so that doesn’t compile. We can of course control the type, for example List.<Object>of("").
@OleV.V. very interesting that Eclipse shows you the inferred type, I've tried javac --debug=verboseResolution=deferred-inference ..., but I don't get a clear picture of what List.of("") is inferred to.
|
1

I think this works for you. However, I had to convert the second array to a set check if element exists in an array. I think that's more intuitive than a for loop iteration.

String[] arr1 = new String[]{"a", "b"};
String[] arr2 = new String[]{"a", "d"};
Set<String> strings = Set.of(arr2);
boolean result = Stream.of(arr1).anyMatch(strings::contains);

1 Comment

you could create a Set for faster look-ups, since java-9 there is that one single method call Set::of, in java-8 - you would still need to do new HashSet<>(Arrays.asList(array))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.