filter table with another set of array elements - Java 8

advertisements

i have a string array like this:

firstArray = {"1", "2", "3", "4" };

and i have second array like this:

secondArray = {"2", "5", "6", "7" };

if i want to stream with one element, i can do like this:

firstArray.stream()
    .filter(element -> !element.equals("2"))
    .forEach((element) -> {
        finalArrayList.add(element);
    }
);

how can i stream first array with second arrays all elements in java 8 ?


If you want to keep only elements of the first array that you don't have in the second array using the Stream API, you could do it like this:

List<String> result = Arrays.stream(firstArray)
    .filter(el -> Arrays.stream(secondArray).noneMatch(el::equals))
    .collect(Collectors.toList());