Arun Khot Arun Khot - 1 year ago 100
Java Question

iterating and filtering two lists using java 8

I want to iterate two lists and get new filtered list which will have values not present in second list. Can anyone help?

I have two lists - one is list of strings, and the other is list of


List<String> list1;
List<MyClass> list2;

MyClass {

MyClass(String val)
this.str = val;

String str;

I want filtered list of strings based on -> check second list for elements (abc) whose values not present in

List<String> list1 = Arrays.asList("abc", "xyz", "lmn");

List<MyClass> list2 = new ArrayList<MyClass>();

MyClass obj = new MyClass("abc");
obj = new MyClass("xyz");

Now I want new filtered list -> which will have value => "lmn". i.e. values not present in
whose elements are in

Answer Source

Finally I got way to achieve this following way -

List<String> unavailable =
                .filter(e -> (
                        .filter(d -> d.getStr().equals(e))

But this is also working as expected. Please let me know how much this is effective? and if anyone has other way to do same thing?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download