1、两个集合求差集
public static List<String> differenceList(List<String> maxList, List<String> minList) {
// 大集合用LinkedList
LinkedList<String> linkedList = new LinkedList<>(maxList);
// 小集合用HashSet
HashSet<String> hashSet = new HashSet<>(minList);
// 采用Iterator迭代器进行遍历
Iterator<String> iterator = linkedList.iterator();
while (iterator.hasNext()) {
if (hashSet.contains(iterator.next())) {
iterator.remove();
}
}
return new ArrayList<>(linkedList);
}
评论区