Java 集合运算(交集、并集和差集)
参考:https://blog.csdn.net/lsp1991/article/details/46931151
1、用法
[List obj1].retainAll([List obj2]); //交集
[List obj1].addAll([List obj2]); //并集
[List obj1].removeAll([List obj2]); //差集,obj1-obj2
- 集合运算的返回值是一个集合
- 集合运算时一般先把目标集合拷贝备份,不然运算后集合元素将被更改,如:
private List union(List l1,List l2) {
List list =new ArrayList(Arrays.asList(new Object[l1.size()]));
Collections.copy(list, l1); //将l1拷贝到list中
list.addAll(l2);
return list;
}
- 在并集运算中,使用 [List obj].addAll([List obj])的方法得到的结果是两个集合中的所有元素,有可能有重复的元素,跟数学上意义上的并集有所差别,欲求数学意义上的并集则需进行转化(因此,一般将元素存储到HashSet或TreeSet中,能保证集合运算结果中元素的唯一性),例如:
/*实际的集合运算结果*/
//print the union of the two list
System.out.println("\n");
System.out.println("Union: ");
list=opt.union(l1, l2);
for(i=0;i<list.size();i++) {
System.out.print(list.get(i)+" ");
}
/*运算结果
Union:
1 2 3 4 6 8 2 4 7 9
*/
- 转换成数学意义上的并集结果
/*转换后变成数学意义上的运算结果*/
//print the union of the two list
System.out.println("\n");
System.out.println("Union: ");
list=opt.union(l1, l2);
for(i=0;i<list.size();i++) {
System.out.print(list.get(i)+" ");
}
//The real union of the two list....
System.out.println("\n");
HashSet unionlist=new HashSet();
for(i=0;i<list.size();i++) {
unionlist.add(list.get(i));
}
System.out.println("The real union of the two list is : ");
Iterator ite=unionlist.iterator();
while(ite.hasNext()) {
System.out.print(ite.next()+" ");
}
/*
Union:
1 2 3 4 6 8 2 4 7 9
The real union of the two list is :
1 2 3 4 6 7 8 9
*/
*注:
- split() 方法根据匹配给定的正则表达式来拆分字符串。
注意: . 、 | 和 * 等转义字符,必须得加 \ 。
注意:多个分隔符,可以用 | 作为连字符 。
2、集合运算实例:
import java.util.*;
public class setOpt {
//The operation of Intersect
private List intersetct(List l1,List l2) {
List list=new ArrayList(Arrays.asList(new Object[l1.size()]));
Collections.copy(list, l1);
list.retainAll(l2);
return list;
}
//The operation of Union
private List union(List l1,List l2) {
List list =new ArrayList(Arrays.asList(new Object[l1.size()]));
Collections.copy(list, l1);
list.addAll(l2);
return list;
}
//The operation of Diff
private List diff(List l1,List l2) {
List list =new ArrayList(Arrays.asList(new Object[l1.size()]));
Collections.copy(list, l1);
list.removeAll(l2);
return list;
}
public static void main(String[] args) {
String str1[]="1;2;3;4;6;8".split(";");
String str2[]="2;4;7;9".split(";");
int i,j;
setOpt opt=new setOpt();
List l1=new ArrayList();
List l2=new ArrayList();
List list;
for(i=0;i<str1.length;i++) {
l1.add(str1[i]);
}
for(j=0;j<str2.length;j++) {
l2.add(str2[j]);
}
//print the intersection of the two list
System.out.println("Intersection: ");
list=opt.intersetct(l1, l2);
for(i=0;i<list.size();i++) {
System.out.print(list.get(i)+" ");
}
//print the union of the two list
System.out.println("\n");
System.out.println("Union: ");
list=opt.union(l1, l2);
for(i=0;i<list.size();i++) {
System.out.print(list.get(i)+" ");
}
//The real union of the two list....
System.out.println("\n");
HashSet unionlist=new HashSet();
for(i=0;i<list.size();i++) {
unionlist.add(list.get(i));
}
System.out.println("The real union of the two list is : ");
Iterator ite=unionlist.iterator();
while(ite.hasNext()) {
System.out.print(ite.next()+" ");
}
//print the diff of the two list
System.out.println("\n");
System.out.println("Diff: ");
list=opt.diff(l1, l2);
for(i=0;i<list.size();i++) {
System.out.print(list.get(i)+" ");
}
}
}
/*运行结果:
Intersection:
2 4
Union:
1 2 3 4 6 8 2 4 7 9
The real union of the two list is :
1 2 3 4 6 7 8 9
Diff:
1 3 6 8
*/
附:
蓝桥杯集合运算算法题(TreeSet存储元素的唯一性求解法):
https://blog.csdn.net/CR_fun/article/details/79294541