会改变原有的List
交集retainAll
差集removeAll
并集removeAll+addAll或者addAll+distinct
不改变原有的List:
//交集
public static <T> List<T> retainAll(List<T> list1, List<T> list2) {
List<T> list = new ArrayList<T>();
list.addAll(list1);
list.retainAll(list2);
return list;
}
//差集
public static <T> List<T> subtraction(List<T> list1, List<T> list2) {
List<T> list = new ArrayList<T>();
list.addAll(list1);
list.removeAll(list2);
return list;
}
//并集
public static <T> List<T> removeDuplicate(List<T> list1, List<T> list2) {
List<T> list = new ArrayList<T>();
list.addAll(list1);
list.addAll(list2);
HashSet<T> hs = new HashSet<T>(list);
list.clear();
list.addAll(hs);
return list;
}
网友评论