Reputation: 2069
I have a list and a String array
String[] B
List<String> A
I want to checkif an element exists in List A, remove it, and then remove elements in array B, which are in list A,
How can it be done in Java 8 using streams, in a single line ?
This is how how remve the element from List A.stream().filter(element -> !element.equalsIgnoreCase(tobeIgnored).collect(Collectors.toList());
Upvotes: 0
Views: 79
Reputation: 2069
seems this is working:
List A
Array B
B = Arrays.stream(B).filter(s -> !(A.stream().filter(el ->
!el.equalsIgnoreCase("ok")).collect(Collectors.toList())).
contains(s)).toArray(String[]::new);
Upvotes: 1
Reputation: 120848
First of all I'd create a HashSet
from A
and a List from the array:
List<String> list = new ArrayList<>(Arrays.asList(B));
Set<String> set = new HashSet<>(A);
And than create the array with only the elements you want:
list.removeIf(set::contains);
list.toArray(new String[0]);
Upvotes: 1