开发者

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

开发者 https://www.devze.com 2023-01-18 23:15 出处:网络
List<String> listA = new ArrayList<String>(); listA.add(\"a\"); listA.add(\"b\"); listA.add(\"c\");
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?


listB.removeAll(listA)

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

0

精彩评论

暂无评论...
验证码 换一张
取 消