Bootstrap

JAVA CollectionUtils常用方法

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

1、差集 subtract

		//数据准备
		List<Integer> list1 = Arrays.asList(1, 2, 5);
        List<Integer> list2 = Arrays.asList(0, 2, 5, 6);

        //list1-list2差集(集合相减) list1扣除list2共同数据
        System.out.println(CollectionUtils.subtract(list1, list2));//[1]

        //list2-list1差集(集合相减) list2扣除list1共同数据
        System.out.println(CollectionUtils.subtract(list2, list1)); //[0, 6]

2、交集 intersection

		//数据准备
		List<Integer> list1 = Arrays.asList(1, 2, 5);
        List<Integer> list2 = Arrays.asList(0, 2, 5, 6);

        //List1、List2交集
        System.out.println(CollectionUtils.intersection(list2, list1)); //[2, 5]

3、并集 union

		//数据准备
		List<Integer> list1 = Arrays.asList(1, 2, 5);
        List<Integer> list2 = Arrays.asList(0, 2, 5, 6);

        //List1、List2并集
        System.out.println(CollectionUtils.union(list1, list2));//[0, 1, 2, 5, 6]

4、交集的补集 disjunction

		//数据准备
		List<Integer> list1 = Arrays.asList(1, 2, 5);
        List<Integer> list2 = Arrays.asList(0, 2, 5, 6);

        //2个数组交集的补集
        System.out.println(CollectionUtils.disjunction(list1, list2));//[0, 1, 6]

5、集合是否相等 isEqualCollection

		//数据准备
		List<Integer> list1 = Arrays.asList(1, 2, 5);
        List<Integer> list2 = Arrays.asList(0, 2, 5, 6);

        //集合是否相等
        System.out.println(CollectionUtils.isEqualCollection(list1,list2));//false

        List<Integer> list3 = Arrays.asList(2, 5);
        List<Integer> list4 = Arrays.asList(5, 2);
        //集合是否相等
        System.out.println(CollectionUtils.isEqualCollection(list3,list4));//true

6、集合判空 isEmpty、isNotEmpty

		//数据准备
		List<Integer> list1 = Arrays.asList(1, 2, 5);
        List<Integer> list2 = Arrays.asList(0, 2, 5, 6);

        //集合是否不为空
        System.out.println(CollectionUtils.isNotEmpty(list1));//true

        //集合为空判断
        System.out.println(CollectionUtils.isEmpty(list1));//false

7、是否全部包含 containsAll

		//数据准备
		List<Integer> list1 = Arrays.asList(1, 2, 5);
        List<Integer> list2 = Arrays.asList(0, 2, 5, 6);
        System.out.println(CollectionUtils.containsAll(list1,list2));//false

 		List<Integer> list1 = Arrays.asList(1, 2, 5);
        List<Integer> list2 = Arrays.asList(0,1, 2,3, 5, 6);
        System.out.println(CollectionUtils.containsAll(list1,list2));//false
        System.out.println(CollectionUtils.containsAll(list2,list1));//true

        
		List<Integer> list3 = Arrays.asList(2, 5);
        List<Integer> list4 = Arrays.asList(5, 2);

        System.out.println(CollectionUtils.containsAll(list3,list4));//true

   

8、是否 任一包含 containsAny

		//数据准备
		List<Integer> list1 = Arrays.asList(1, 2, 5);
        List<Integer> list2 = Arrays.asList(0, 2, 5, 6);
		List<Integer> list3 = Arrays.asList(2, 5);
        List<Integer> list4 = Arrays.asList(5, 2);
        List<Integer> list5 = Arrays.asList(8, 9);

        List<List<Integer>> all = Lists.newArrayList(Arrays.asList(1, 2, 5), Arrays.asList(2, 5));

        System.out.println(CollectionUtils.containsAny(all,list2,list1));//true
        System.out.println(CollectionUtils.containsAny(all,list2,list5));//false
        System.out.println(CollectionUtils.containsAny(list1,7,6));//false
        System.out.println(CollectionUtils.containsAny(list1,2,6));//true
        System.out.println(CollectionUtils.containsAny(list2,list1));//true

9、是否为子集 isSubCollection(coll1,coll2)

判断coll1是否为coll2子集

        List<Integer> list1 = Arrays.asList(1, 2, 5);
        List<Integer> list3 = Arrays.asList(2, 5);
        System.out.println(CollectionUtils.isSubCollection(list3,list1));//true
       
;