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 ...
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); 52 list.addAll(ls2); 53 return list; 54 } 55 }View Code