What exactly do you want to do? Get all the elements in the first array that are also present in the second array?
Like this:
String[] firstArray = {"1", "2", "3", "4"};
String[] secondArray = {"2", "5", "6", "7"};
List<String> finalArrayList = Arrays.stream(firstArray)
.filter(element -> arrayContains(secondArray, element))
.collect(Collectors.toList());
Using the following utility method:
public static <T> boolean arrayContains(T[] array, T element) {
return Arrays.stream(array)
.filter(e -> e.equals(element))
.findFirst().isPresent();
}
Note: Instead of using forEach and adding the results to finalArrayList, use a collector. That's more pure, from a functional programming point of view.
edit - With Holger's tips (see his comment below):
public static <T> boolean arrayContains(T[] array, T element) {
return Arrays.stream(array)
.anyMatch(e -> e.equals(element));
}
Stream.concat(Stream, Stream)docs.oracle.com/javase/8/docs/api/java/util/stream/…