package commons之集合与队列;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.collections4.CollectionUtils;
/**
- 集合操作
- 1.交集:CollectionUtils.union( );
- 2.并集:CollectionUtils.intersection()
-
CollectionUtils.retainAll()
- `3.差集:CollectionUtils.subtract()
*/
public class Demo01 {
public static void main(String[] args) {
//容器
Set set1 = new HashSet();
set1.add(1);
set1.add(2);
set1.add(3);
Set set2 = new HashSet();
set2.add(2);
set2.add(3);
set2.add(4);
//并集
System.out.println("=并集========");
Collection col = CollectionUtils.union(set1, set2);//union并集
for(Integer temp:col){
System.out.println(temp);
}
//交集
System.out.println("=交集========");
//col = CollectionUtils.intersection(set1, set2);//intersection交集
//另一种方法
col = CollectionUtils.retainAll(set1, set2);
for(Integer temp:col){
System.out.println(temp);
}
//差集
System.out.println("=差集========");
col = CollectionUtils.subtract(set1, set2);//subtract差集
for(Integer temp:col){
System.out.println(temp);
}
}
}
//结果-----------------------------------------
=并集========
1
2
3
4
=交集========
2
3
=差集========
1