Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you remove the overlapping contents of one List from another List?

List<String> listA = new ArrayList<String>();
listA.add("a");
listA.add("b");
listA.add("c");
listA.add("d");



List<String> listB = new ArrayList<String>();
listB.add("c");
listB.add("d");
listB.add("e");
listB.add("f");

ListB contains two elements that are also present in ListA ("c" and "d").

Is there a clean way to make sure that listB does not contain these or any other overlapping elements that may already exist in listA?

like image 555
jts Avatar asked Oct 02 '10 15:10

jts


1 Answers

listB.removeAll(listA)

This would make your listB contain only [e, f].

like image 94
Bozho Avatar answered Nov 05 '22 16:11

Bozho