CollectionUtils 操作集

package com.jakin.util; import java.util.ArrayList; 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 CollectionUtilsTest { @SuppressWarnings("unchecked") public static void main(String[] args) { List aList = new ArrayList(); aList.add("aaa"); aList.add("bbb"); aList.add("ccc"); List bList = new ArrayList(); bList.add("aaa"); bList.add("ddd"); bList.add("eee"); // 并集 Collection unionList = CollectionUtils.union(aList, bList); // 交集 Collection intersectionList = CollectionUtils.intersection(aList, bList); // 是否存在交集 boolean isContained = CollectionUtils.containsAny(aList, bList); // 交集的补集 Collection disjunctionList = CollectionUtils.disjunction(aList, bList); // 集合相减 Collection subtractList = CollectionUtils.subtract(aList, bList); // 排序 Collections.sort((List) unionList); Collections.sort((List) intersectionList); Collections.sort((List) disjunctionList); Collections.sort((List) subtractList); // 测试 System.out.println("A: " + ArrayUtils.toString(aList.toArray())); System.out.println("B: " + ArrayUtils.toString(bList.toArray())); System.out.println("A has one of B? : " + isContained); System.out.println("Union(A, B): " + ArrayUtils.toString(unionList.toArray())); System.out.println("Intersection(A, B): " + ArrayUtils.toString(intersectionList.toArray())); System.out.println("Disjunction(A, B): " + ArrayUtils.toString(disjunctionList.toArray())); System.out.println("Subtract(A, B): " + ArrayUtils.toString(subtractList.toArray())); } }

你可能感兴趣的:(经验积累)