ListUtils类代码示例

简介: ListUtils类代码示例

引入依赖

 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>

方法代码示例

defaultIfNull(List list, List defaultList)

/**
     * defaultIfNull(List<T> list, List<T> defaultList)
     * Returns either the passed in list, or if the list is {@code null},
     * the value of {@code defaultList}.
     * 如果是null,则返回默认列表
     */
    @Test
    void test1() {

        // result1 = []
        List<String> list = new ArrayList<>();
        List<String> defaultList = Arrays.asList("ben", "ben", "xiong");
        List<String> result = ListUtils.defaultIfNull(list, defaultList);
        System.out.println("result1 = " + result);

        // result2 = [ben, ben, xiong]
        list = null;
        result = ListUtils.defaultIfNull(list, defaultList);
        System.out.println("result2 = " + result);
    }

emptyIfNull(List list)

/**
     * emptyIfNull(List<T> list)
     * Returns an immutable empty list if the argument is null, or the argument itself otherwise.
     * 返回一个不可改变的空列表
     */
    @Test
    void test2() {

        //result3 =[ben, ben, xiong]
        List<String> list = Arrays.asList("ben", "ben", "xiong");
        List<String> result = ListUtils.emptyIfNull(list);
        System.out.println("result3 = " + result);

        //result4 = []
        result = ListUtils.emptyIfNull(null);
        System.out.println("result4 = " + result);

        //result5 = []
        list = null;
        result = ListUtils.emptyIfNull(list);
        System.out.println("result5 = " + result);

        //异常:java.lang.UnsupportedOperationException
        result.add("name");
        System.out.println("result6 = " + result);
    }

fixedSizeList(List list)

/**
     * fixedSizeList(List<E> list)
     * Returns a fixed-sized list backed by the given list.
     * 返回一个固定大小的列表,返回的列表不可增加或删除元素,但是可以修改元素
     */
    @Test
    public void test3() {
        // result7 = [ben, ben, xiong]
        List<String> list = Arrays.asList("ben", "ben", "xiong");
        List<String> result = ListUtils.fixedSizeList(list);
        System.out.println("result7 = " + result);

        // 异常: java.lang.UnsupportedOperationException: List is fixed size
        //result.add("newEle");
        // System.out.println("result = " + result);[视频号]Happiness5307的动态

        // 异常: java.lang.UnsupportedOperationException: List is fixed size
        //result.remove(0);
        //System.out.println("result = " + result);

        // result7 = [ben, ben, xiong2]
        result.set(2, "xiong2");
        System.out.println("result = " + result);
    }

hashCodeForList(Collection<?> list)

/**
     * hashCodeForList(Collection<?> list)
     * Generates a hash code using the algorithm specified in List.hashCode().
     * 使用List.hashCode()中指定的算法生成一个哈希代码。
     */
    @Test
    public void test4() {
        //hashCode = 210730198
        List<String> list = Arrays.asList("ben", "ben", "xiong");
        int hashCode = ListUtils.hashCodeForList(list);
        System.out.println("hashCode = " + hashCode);
    }

indexOf(List list, Predicate predicate)

/**
     * indexOf(List<E> list, Predicate<E> predicate)
     * Finds the first index in the given List which matches the given predicate.
     * 返回第一个根据predicate找到的索引
     * predicate是collections4包中自带的
     */

    @Test
    public void test5() {
        // index = 0
        List<String> list = Arrays.asList("ben", "ben", "xiong");
        list.add("fei");
        System.out.println(list);
        Predicate<String> p = e -> e.length() == 3;
        int index = ListUtils.indexOf(list, p);
        System.out.println("index = " + index);
    }

intersection(List<? extends E> list1, List<? extends E> list2)

/**
     * intersection(List<? extends E> list1, List<? extends E> list2)
     * Returns a new list containing all elements that are contained in both given lists.
     * 返回两个列表的交集,如果找不到交集,则返回空集,而不是null
     */
    @Test
    public void Test6() {
        //result1 = [3, 4, 5, 6]
        //result2 = []
        List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5, 6);
        List<Integer> list2 = Arrays.asList(3, 4, 5, 6, 7, 8);
        List<Integer> list3 = Arrays.asList(7, 8, 9, 10);
        List<Integer> res_list1 = ListUtils.intersection(list1, list2);
        List<Integer> res_list2 = ListUtils.intersection(list1, list3);
        System.out.println("result1 = " + res_list1);
        System.out.println("result2 = " + res_list2);
    }

isEqualList(Collection<?> list1, Collection<?> list2)

/**
     * isEqualList(Collection<?> list1, Collection<?> list2)
     * Tests two lists for value-equality as per the equality contract in List.equals(java.lang.Object).
     * 比较两个列表的所有对应元素是否都相等,如果都相等返回true,如果有不相等或者元素个数不一致则返回false
     */
    @Test
    public void test7() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
        boolean equalList = ListUtils.isEqualList(list, list1);
        System.out.println(equalList);

        List<String> list2 = Arrays.asList("A", "B", "C");
        List<String> list3 = Arrays.asList("A", "B", "C");
        List<String> list4 = Arrays.asList("A", "C", "B");
        System.out.println(ListUtils.isEqualList(list2, list3));
        System.out.println(ListUtils.isEqualList(list2, list4));
    }

longestCommonSubsequence(CharSequence a, CharSequence b)

/**
     * longestCommonSubsequence(CharSequence a, CharSequence b)
     * Returns the longest common subsequence (LCS) of two CharSequence objects.
     * 返回两个字符串中公共字符并组成新串
     */
    @Test
    public void test10() {
        CharSequence cs = "hello";
        CharSequence cs1 = "llogu";
        System.out.println(ListUtils.longestCommonSubsequence(cs, cs1));
    }

longestCommonSubsequence(List a, List b)

/**
     * longestCommonSubsequence(List<E> a, List<E> b)
     * Returns the longest common subsequence (LCS) of two sequences (lists).
     * 返回两个列表中的公共元素,并组成新列表
     */
    @Test
    public void test11() {
        List<String> list = Arrays.asList("A", "B", "C", "D");
        List<String> list1 = Arrays.asList("B", "C");
        List<String> res_list = ListUtils.longestCommonSubsequence(list, list1);
        System.out.println("result = " + res_list);
    }

longestCommonSubsequence(List a, List b, Equator<? super E> equator)

/**
     * longestCommonSubsequence(List<E> a, List<E> b, Equator<? super E> equator)
     * Returns the longest common subsequence (LCS) of two sequences (lists).
     * 返回两个序列(列表)的最长公共子序列(LCS)。
     */
    @Test
    public void test12() {
        Equator<String> equator = new Equator<String>() {
            @Override
            public boolean equate(String o1, String o2) {
                return o1.equals(o2);
            }

            @Override
            public int hash(String o) {
                return o.hashCode();
            }
        };

        List<String> list1 = Arrays.asList("2", "1", "4", "3", "5", "6", "3");
        List<String> list2 = Arrays.asList("1", "3", "7");

        List<String> result = ListUtils.longestCommonSubsequence(list1, list2, equator);
        System.out.println("result = " + result); // result = [1, 3]
    }

partition(List list, int size)

/**
     * partition(List<T> list, int size)
     * Returns consecutive sublists of a list, each of the same size (the final list may be smaller).
     * 返回列表的连续子列表,每个子列表的大小相同(最终列表可能更小)。
     */
    @Test
    public void test13() {
        List<String> list = new ArrayList<>();
        String[] arr = {"1", "2", "3", "4", "5"};
        CollectionUtils.addAll(list, arr);

        List<List<String>> result = ListUtils.partition(list, 3);
        System.out.println("result = " + result); // result = [[1, 2, 3], [4, 5]]
    }

predicatedList(List list, Predicate predicate)

/**
     * predicatedList(List<E> list, Predicate<E> predicate)
     * Returns a predicated (validating) list backed by the given list.
     * 校验整个列表元素是否都满足指定的规则
     */
    @Test
    public void test14() {
        List<Integer> list = new ArrayList<>();
        Integer[] arr = {3, 4, 5};
        /*for (Integer a : arr) {
            list.add(a);
        }*/
        CollectionUtils.addAll(list,arr);

        Predicate<Integer> predicate = e -> e.intValue() >= 3;
        List<Integer> result = ListUtils.predicatedList(list, predicate);
        System.out.println("result = " + result); // result = [3, 4, 5]
        predicate = e -> e.intValue() >= 4;
        result = ListUtils.predicatedList(list, predicate);
        System.out.println("result = " + result); // java.lang.IllegalArgumentException: Cannot add Object '3'
    }

removeAll(Collection collection, Collection<?> remove)

/**
     * removeAll(Collection<E> collection, Collection<?> remove)
     * Removes the elements in remove from collection.
     * 两个列表相减,返回差集,原两个列表保持不变
     */
    @Test
    public void test15() {
        List<String> list1 = Arrays.asList("1", "2", "3", "4", "5");
        List<String> list2 = Arrays.asList("2", "3", "4");
        List<String> result = ListUtils.removeAll(list1, list2);
        System.out.println("list1 = " + list1); // list1 = [1, 2, 3, 4, 5]
        System.out.println("list2 = " + list2); // list2 = [2, 3, 4]
        System.out.println("result = " + result); // result = [1, 5]
    }

retainAll(Collection collection, Collection<?> retain)

/**
     *     retainAll(Collection<E> collection, Collection<?> retain)
     * Returns a List containing all the elements in collection that are also in retain.
     * 取交集,和intersection比较
     */
    @Test
    public void test16() {
        List<String> list1 = Arrays.asList("1", "2", "3", "4", "5");
        List<String> list2 = Arrays.asList("2", "3", "41");
        List<String> result = ListUtils.retainAll(list1, list2);
        System.out.println("list1 = " + list1); // list1 = [1, 2, 3, 4, 5]
        System.out.println("list2 = " + list2); // list2 = [2, 3, 41]
        System.out.println("result = " + result); // result = [2, 3]

        result = ListUtils.intersection(list1, list2);
        System.out.println("result = " + result); // result = [2, 3]
    }

select(Collection<? extends E> inputCollection, Predicate<? super E> predicate)

/**
     * select(Collection<? extends E> inputCollection, Predicate<? super E> predicate)
     * Selects all elements from input collection which match the given predicate into an output list.
     * 相当于stream的filter + toList功能
     */
    @Test
    public void test17() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = ListUtils.select(list, e -> e > 2);
        System.out.println("result = " + result); // result = [3, 4, 5]
        System.out.println("list = " + list); // list = [1, 2, 3, 4, 5]
    }

selectRejected(Collection<? extends E> inputCollection, Predicate<? super E> predicate)

/**
     * selectRejected(Collection<? extends E> inputCollection, Predicate<? super E> predicate)
     * Selects all elements from inputCollection which don't match the given predicate into an output collection.
     * 相当于ListUtils.select的补集
     */
    @Test
    public void test18() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = ListUtils.selectRejected(list, e -> e > 2);
        System.out.println("result = " + result); // result = [1, 2]
    }

subtract(List list1, List<? extends E> list2)

/**
     * subtract(List<E> list1, List<? extends E> list2)
     * Subtracts all elements in the second list from the first list, placing the results in a new list.
     * 第一个list减去第二个list;原来两个list保持不变
     */
    @Test
    public void test19() {
        List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> list2 = Arrays.asList(2, 3, 4);
        List<Integer> result = ListUtils.subtract(list1, list2);
        System.out.println("list1 = " + list1); // list1 = [1, 2, 3, 4, 5]
        System.out.println("list2 = " + list2); // list2 = [2, 3, 4]
        System.out.println("result = " + result); // result = [1, 5]
    }

sum(List<? extends E> list1, List<? extends E> list2)

/**
     *     sum(List<? extends E> list1, List<? extends E> list2)
     * Returns the sum of the given lists.
     * 返回一个不重复的列表
     */
    @Test
    public void test20() {
        List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5, 3);
        List<Integer> list2 = Arrays.asList(2, 3, 4);
        List<Integer> result = ListUtils.sum(list1, list2);
        System.out.println("result = " + result); // result = [1, 5, 3, 2, 3, 4]
    }

synchronizedList(List list)

/**
     * synchronizedList(List<E> list)
     * Returns a synchronized list backed by the given list.
     * 返回由给定列表支持的同步列表
     */
    @Test
    public void test21() {
        List<String> list = Arrays.asList("ben", "ben", "xiong");
        List<String> result = ListUtils.synchronizedList(list);
        System.out.println("result = " + result); // result = [ben, ben, xiang]
        //修改list,result也会改变
        list.set(0,"ben1");
        System.out.println("result = " + result);
    }

transformedList(List list, Transformer<? super E,? extends E> transformer)

/**
     * transformedList(List<E> list, Transformer<? super E,? extends E> transformer)
     * Returns a transformed list backed by the given list.
     * 注意:Transformer<? super E,? extends E>
     */
    @Test
    public void test22() {
        List<String> list = new ArrayList<>();
        String[] arr = {"ben", "ben", "xiong"};
        CollectionUtils.addAll(list, arr);

        List<String> result = ListUtils.transformedList(list, CharSequence::toString);
        System.out.println("result = " + result); // result = [ben, ben, xiong]
    }

union(List<? extends E> list1, List<? extends E> list2)

/**
     * union(List<? extends E> list1, List<? extends E> list2)
     * Returns a new list containing the second list appended to the first list.
     * 真正的相加,第二个list追加到第一个list的后面,不会去重
     */
    @Test
    public void test23() {
        List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> list2 = Arrays.asList(2, 3, 4);
        List<Integer> result = ListUtils.union(list1, list2);
        System.out.println("result = " + result); // result = [1, 2, 3, 4, 5, 2, 3, 4]
    }

unmodifiableList(List<? extends E> list)

/**
     * unmodifiableList(List<? extends E> list)
     * Returns an unmodifiable list backed by the given list.
     * 不可添加,但可以删除和修改值
     */
    @Test
    public void test24() {
        List<Integer> list = new ArrayList<>();
        Integer[] arr = {1, 2, 3, 4, 5};
        CollectionUtils.addAll(list, arr);
        System.out.println("list = " + list); // list = [1, 2, 3, 4, 5]
        List<Integer> result = ListUtils.unmodifiableList(list);
        list.remove(new Integer(5));
        list.set(0, 2);
        System.out.println("result = " + result); // result = [2, 2, 3, 4]
        result.add(6); // java.lang.UnsupportedOperationException
    }
目录
相关文章
|
3月前
|
Java
【Java】一个简单的接口例子(帮助理解接口+多态)
【Java】一个简单的接口例子(帮助理解接口+多态)
34 0
|
3月前
|
编译器 C++
C++模板之——类模板详解及代码示例
C++模板之——类模板详解及代码示例
C++模板之——类模板详解及代码示例
|
3月前
|
C++
C++多态详解及代码示例
C++多态详解及代码示例
|
2月前
|
Java
Java中代码块区别及代码示例
Java中代码块区别及代码示例
15 0
|
11月前
|
缓存 安全 Java
Java 单例模式讲解和代码示例
Java 单例模式讲解和代码示例
79 0
|
11月前
|
Java
Java 生成器模式讲解和代码示例
Java 生成器模式讲解和代码示例
57 0
|
11月前
|
Go
Go 生成器模式讲解和代码示例
Go 生成器模式讲解和代码示例
84 0
|
11月前
|
Java 数据安全/隐私保护
Java 装饰模式讲解和代码示例
Java 装饰模式讲解和代码示例
68 0
|
11月前
|
设计模式 Java
JAVA 适配器模式讲解和代码示例
JAVA 适配器模式讲解和代码示例
111 0
|
11月前
|
Java
Java 组合模式讲解和代码示例
Java 组合模式讲解和代码示例
57 0