• CollectionUtils工具类的一些方法介绍


      在有了java8,List的操作更加多样化,但是关于这个工具类,还是很好用。今天遇到一个两个集合的比较方法,不是很熟。刚好,对这个工具类的常见方法做一些梳理。

      在网上看了,都是相同的程序,验证了一下,没有问题,学习了,并记录,没有进行新的程序书写。

      感觉这些知识点够用了。

     1.引入依赖

    <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-collections4</artifactId>
                <version>4.1</version>
            </dependency>
    

      

    2.并集

        /**
         * 2个数组取并集
         * [A, B, C, D, E, F, G, H, K]
         */
        @Test
        public void testUnion(){
            String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };
            String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
            List<String> listA = Arrays.asList(arrayA);
            List<String> listB = Arrays.asList(arrayB);
            System.out.println(CollectionUtils.union(listA, listB));
        }
    

      

    3.交集

        /**
         * 2个数组取交集
         * [B, D, F]
         */
        @Test
        public void testIntersection(){
            String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };
            String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
            List<String> listA = Arrays.asList(arrayA);
            List<String> listB = Arrays.asList(arrayB);
            System.out.println(CollectionUtils.intersection(listA, listB));
        }
    

      

    4.交集的补集

        /**
         * 交集的补集(析取)
         * [A, C, E, G, H, K]
         */
        @Test
        public void testDisjunction(){
            String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };
            String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
            List<String> listA = Arrays.asList(arrayA);
            List<String> listB = Arrays.asList(arrayB);
            System.out.println(CollectionUtils.disjunction(listA, listB));
        }
    

      

    5.差集

        /**
         * 差集(扣除)
         * [A, C, E]
         */
        @Test
        public void testSubtract(){
            String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };
            String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
            List<String> listA = Arrays.asList(arrayA);
            List<String> listB = Arrays.asList(arrayB);
            System.out.println(CollectionUtils.subtract(listA, listB));
        }
    

      

    6.是否为空

    /**
         * 是否为空
         */
        @Test
        public void testIsEmpty(){
            class Person{}
            class Girl extends Person{}
            List<Integer> first = new ArrayList<>();
            List<Integer> second = null;
            List<Person> boy = new ArrayList<>();
            //每个男孩心里都装着一个女孩
            boy.add(new Girl());
            //判断集合是否为空
            System.out.println(CollectionUtils.isEmpty(first));   //true
            System.out.println(CollectionUtils.isEmpty(second));   //true
            System.out.println(CollectionUtils.isEmpty(boy));   //false
    
            //判断集合是否不为空
            System.out.println(CollectionUtils.isNotEmpty(first));   //false
            System.out.println(CollectionUtils.isNotEmpty(second));   //false
            System.out.println(CollectionUtils.isNotEmpty(boy));   //true
        }
    

      

    7.是否相等

      感觉还是很实用。

        /**
         * 集合是否相等
         */
        @Test
        public void testIsEqual(){
            class Person{}
            class Girl extends Person{
            }
    
            List<Integer> first = new ArrayList<>();
            List<Integer> second = new ArrayList<>();
            first.add(1);
            first.add(2);
            second.add(2);
            second.add(1);
            Girl goldGirl = new Girl();
            List<Person> boy1 = new ArrayList<>();
            //每个男孩心里都装着一个女孩
            boy1.add(new Girl());
            List<Person> boy2 = new ArrayList<>();
            //每个男孩心里都装着一个女孩
            boy2.add(new Girl());
            //比较两集合值
            System.out.println(CollectionUtils.isEqualCollection(first,second));   //true
            System.out.println(CollectionUtils.isEqualCollection(first,boy1));   //false
            System.out.println(CollectionUtils.isEqualCollection(boy1,boy2));   //false
    
            List<Person> boy3 = new ArrayList<>();
            //每个男孩心里都装着一个女孩
            boy3.add(goldGirl);
            List<Person> boy4 = new ArrayList<>();
            boy4.add(goldGirl);
            System.out.println(CollectionUtils.isEqualCollection(boy3,boy4));   //true
        }
    

      

  • 相关阅读:
    CentOS8安装Mysql5.7
    CentOS8搭建FTP服务器
    CentOS8安装jdk1.8
    基于可穿戴设备的医疗监护系统
    air530GPS数据通过air202上传阿里云
    bzoj2594: [Wc2006]水管局长数据加强版
    bzoj3091: 城市旅行
    Problem A. Array Factory XVII Open Cup named after E.V. Pankratiev Stage 4: Grand Prix of SPb, Sunday, Octorber 9, 2016
    hdu5716
    bzoj2002: [Hnoi2010]Bounce 弹飞绵羊
  • 原文地址:https://www.cnblogs.com/juncaoit/p/12422380.html
Copyright © 2020-2023  润新知