Parameswar
Parameswar

Reputation: 2069

Remove an element from List A, Compare a list A and an String Array B and remove elements from array B, which are in List A,

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

Answers (2)

Parameswar
Parameswar

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

Eugene
Eugene

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

Related Questions