import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class ListUntil {
public static void main(String[] args) {
ArrayList arraylist1 = new ArrayList();
ArrayList arraylist2 = new ArrayList();
arraylist1.add(1);
arraylist1.add(2);
arraylist1.add(3);
arraylist2.add(3);
arraylist2.add(4);
List list=ObjectArrayListIntersections(arraylist1,arraylist2);
System.out.println("result" + list);
}
// 两个List集合集求差集
public static ArrayList ObjectArrayListDifference(ArrayList arraylist1, ArrayList arraylist2) {
arraylist1.removeAll(arraylist2);
return arraylist1;
}
// 两个List集合集求并集
public static List ObjectArrayListUnion(ArrayList arraylist1, ArrayList arraylist2) {
List arraylist = new ArrayList();
arraylist.addAll(arraylist1);
arraylist.addAll(arraylist2);
arraylist = new ArrayList(new HashSet(arraylist));
return arraylist;
}
// 两个List集合集求交集
public static ArrayList ObjectArrayListIntersections(ArrayList arraylist1,
ArrayList arraylist2) {
arraylist1.retainAll(arraylist2);
return arraylist1;
}
}
--------------------------------------------------------------------------------------------
JAVA求字符串数组交集、并集和差集
http://blog.sina.com.cn/s/blog_4934a04a0100fqmf.html
http://suko.iteye.com/blog/2053654
Setset1 = new HashSet<>();
Setset2 = new HashSet<>();
set1.add("a");
set1.add("b");
set1.add("c");
set2.add("c");
set2.add("b");
set2.add("e");
// 交集
// set1.retainAll(set2);
// System.out.println("交集是 "+set1);
// 并集
// set1.addAll(set2);
//System.out.println("并集是 " + set1);
// 差集
set1.removeAll(set2);
System.out.println("差集是 " + set1);
网友评论