# ListUtil(差集、交加、并集)

www.MyException.Cn  网友分享于：2013-08-16  浏览：0次
ListUtil(差集、交集、并集)
``` 1 package cn.fraudmetrix.octopus.horai.biz.utils;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.List;
7
8 /**
9  * Created by hunt on 2017/7/31.
10  */
11 @SuppressWarnings("all")
12 public class ListUtil {
13     /**
14      * 求ls对ls2的差集,即ls中有，但ls2中没有的
15      *
16      * @param ls
17      * @param ls2
18      * @return
19      */
20     public static List diff(List ls, List ls2) {
21         List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
22         Collections.copy(list, ls);
23         list.removeAll(ls2);
24         return list;
25     }
26
27     /**
28      * 求2个集合的交集
29      *
30      * @param ls
31      * @param ls2
32      * @return
33      */
34     public static List intersect(List ls, List ls2) {
35         List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
36         Collections.copy(list, ls);
37         list.retainAll(ls2);
38         return list;
39     }
40
41     /**
42      * 求2个集合的并集
43      *
44      * @param ls
45      * @param ls2
46      * @return
47      */
48     public static List union(List ls, List ls2) {
49         List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
50         Collections.copy(list, ls);//将ls的值拷贝一份到list中
51         list.removeAll(ls2);