apache commons collections CollectionUtils工具类简单使用

一. CollectionUtils提供很多对集合的操作方法,常用的方法如下:

集合判断:
例1: 判断集合是否为空:
CollectionUtils.isEmpty(null): true
CollectionUtils.isEmpty(new ArrayList()): true
CollectionUtils.isEmpty({a,b}): false

例2: 判断集合是否不为空:
CollectionUtils.isNotEmpty(null): false
CollectionUtils.isNotEmpty(new ArrayList()): false
CollectionUtils.isNotEmpty({a,b}): true

例3 2个集合间的操作:
集合a: {1,2,3,3,4,5}
集合b: {3,4,4,5,6,7}
CollectionUtils.union(a, b)(并集): {1,2,3,3,4,4,5,6,7}
CollectionUtils.intersection(a, b)(交集): {3,4,5}
CollectionUtils.disjunction(a, b)(交集的补集): {1,2,3,4,6,7}
CollectionUtils.disjunction(b, a)(交集的补集): {1,2,3,4,6,7}
CollectionUtils.subtract(a, b)(A与B的差): {1,2,3}
CollectionUtils.subtract(b, a)(B与A的差): {4,6,7}

代码如下:

package collections_utils;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @Package: collections_utils
 * @ClassName: TestCollectionUtils
 * @Author: ZDG
 * @Date: 2020/8/11 11:37
 */
public class TestCollectionUtils {


    private final static String[] arrayA = new String[]{"A", "B", "C", "D", "E", "F"};
    private final static String[] arrayB = new String[]{"B", "D", "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);
        //2个数组取并集
        System.out.println(ArrayUtils.toString(CollectionUtils.union(listA, listB)));
        //[A, B, C, D, E, F, G, H, K]
    }


    @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);
        //2个数组取交集
        System.out.println(ArrayUtils.toString(CollectionUtils.intersection(listA, listB)));
        //[B, D, F]
    }

    @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);
        //2个数组取交集 的补集
        System.out.println(ArrayUtils.toString(CollectionUtils.disjunction(listA, listB)));
        //[A, C, E, G, H, K]
    }


    @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);
        //arrayA扣除arrayB
        System.out.println(ArrayUtils.toString(CollectionUtils.subtract(listA, listB))); //A与B的差
        //[A, C, E]

        System.out.println(ArrayUtils.toString(CollectionUtils.subtract(listB, listA))); //B与A的差

        //[G,H,K]
    }


    @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
    }



    @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

        /*
        * boy1.add(new Girl());
        *
        * boy2.add(new Girl());
        *
        * new 的地址址不一样的
        *
        * */

        List<Person> boy3 = new ArrayList<>();
        //每个男孩心里都装着一个女孩
        boy3.add(goldGirl);
        List<Person> boy4 = new ArrayList<>();
        boy4.add(goldGirl);
        System.out.println(CollectionUtils.isEqualCollection(boy3,boy4));   //true
    }


    /*
    不可修改的集合
    我们对c进行操作,s也同样获得了和c相同的内容,这样就可以避免其他人员修改这个s对象。
    有时候需要对它进行保护,避免返回结果被人修改。
    */
    @Test
    public void testUnmodifiableCollection(){
        Collection<String> c = new ArrayList<>();
        Collection<String> s = CollectionUtils.unmodifiableCollection(c); //unmodifiableCollection 不可修改的收藏
        c.add("boy");
        c.add("love");
        c.add("girl");
        //! s.add("have a error");  //s 不可被修改的
        System.out.println(s);
    }
   /*
    Collections.unmodifiableCollection可以得到一个集合的镜像,它的返回结果是不可直接被改变,否则会提示错误

    java.lang.UnsupportedOperationException
    at org.apache.commons.collections.collection.UnmodifiableCollection.add(UnmodifiableCollection.java:75)
   */
}

依赖坐标: 点击下载jar

maven 

<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.2</version>
</dependency>

gradle 
// https://mvnrepository.com/artifact/commons-collections/commons-collections
compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2'

你可能感兴趣的:(工具,java)