取得集合list的交集并集(javaAPI或者apache的API)

javaAPI方式----------->
/**
 * 求ls对ls2的差集,即ls中有,但ls2中没有的
 *
 * @param ls
 * @param ls2
 * @return
 */
public static List diff(List ls, List ls2) {
    List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
    Collections.copy(list, ls);
    list.removeAll(ls2);
    return list;
}

/**
 * 求2个集合的交集
 *
 * @param ls
 * @param ls2
 * @return
 */
public static List intersect(List ls, List ls2) {
    List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
    Collections.copy(list, ls);
    list.retainAll(ls2);
    return list;
}

/**
 * 求2个集合的并集
 *
 * @param ls
 * @param ls2
 * @return
 */
public static List union(List ls, List ls2) {
    List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
    Collections.copy(list, ls);//将ls的值拷贝一份到list中
    list.removeAll(ls2);
    list.addAll(ls2);
    return list;
}
apacheAPI方式---------->
使用 CollectionUtils 中四个方法之一执行集合操作.这四种分别是 union(),intersection();disjunction(); subtract(); 下列例子就是演示了如何使用上述四个方法处理两个 Collection; 注: 这些方法都是数学的集合算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import  java.util.Arrays;   
import  java.util.Collection;   
import  java.util.Collections;   
import  java.util.List;   
     
import  org.apache.commons.collections.CollectionUtils;   
import  org.apache.commons.lang.ArrayUtils;   
     
public  class  CollectionUtilsIntro { 
  @SuppressWarnings ( "unchecked" )   
  public  static  void  main(String[] args) {   
   String[] arrayA =  new  String[] {  "1" "2" "3" "3" "4" "5"  };   
   String[] arrayB =  new  String[] {  "3" "4" "4" "5" "6" "7"  };   
     
   List a = Arrays.asList(arrayA);   
   List b = Arrays.asList(arrayB);   
   //并集   
   Collection union = CollectionUtils.union(a, b);   
   //交集   
   Collection intersection = CollectionUtils.intersection(a, b);   
   //交集的补集   
   Collection disjunction = CollectionUtils.disjunction(a, b);   
   //集合相减   
   Collection subtract = CollectionUtils.subtract(a, b);   
     
   Collections.sort((List) union);   
   Collections.sort((List) intersection);   
   Collections.sort((List) disjunction);   
   Collections.sort((List) subtract);   
     
   System.out.println( "A: "  + ArrayUtils.toString(a.toArray()));   
   System.out.println( "B: "  + ArrayUtils.toString(b.toArray()));   
   System.out.println( "--------------------------------------------" );   
   System.out.println( "Union(A, B): "  + ArrayUtils.toString(union.toArray()));   
   System.out.println( "Intersection(A, B): "  + ArrayUtils.toString(intersection.toArray()));   
   System.out.println( "Disjunction(A, B): "  + ArrayUtils.toString(disjunction.toArray()));   
   System.out.println( "Subtract(A, B): "  + ArrayUtils.toString(subtract.toArray()));   
  }   
}
输出如下: A: {1,2,3,3,4,5} B: {3,4,4,5,6,7} -------------------------------------------- Union(A, B): {1,2,3,3,4,4,5,6,7} Intersection(A, B): {3,4,5} Disjunction(A, B): {1,2,3,4,6,7} Subtract(A, B): {1,2,3}

你可能感兴趣的:(java基础)