public class test4 {//Commons集合操作
public static void main(String[] args) {
Set<Integer> set1=new HashSet<Integer>();
set1.add(1);
set1.add(2);
set1.add(3);
set1.add(4);
Set<Integer> set2=new HashSet<Integer>();
set2.add(3);
set2.add(4);
set2.add(5);
set2.add(6);
//并集
Collection<Integer> col=CollectionUtils.union(set1, set2);
for(Integer temp:col){
System.out.print(temp+" ");
}
System.out.println();
//交集
col=CollectionUtils.intersection(set1, set2);
//col=CollectionUtils.retainAll(set1, set2);
for(Integer temp:col){
System.out.print(temp+" ");
}
System.out.println();
//差集
col=CollectionUtils.subtract(set2, set1);
for(Integer temp:col){
System.out.print(temp+" ");
}
}
}
[Commons]——集合操作
最新推荐文章于 2018-09-05 15:56:30 发布