ListUtils

/**
 * defaultIfNull(List<T> list, List<T> defaultList)
 * Returns either the passed in list, or if the list is null, the value of defaultList.
 * 如果是null,则返回默认列表
 */
@Test
public void test1() {
    // case 1:
    List<String> list = new ArrayList<>();
    List<String> defaultList = Arrays.asList("zheng", "hui", "xiang");
    List<String> result = ListUtils.defaultIfNull(list, defaultList);
    System.out.println("result1 = " + result); // result1 = []

    // case 2:
    list = null;
    result = ListUtils.defaultIfNull(list, defaultList);
    System.out.println("result2 = " + result); // result2 = [zheng, hui, xiang]
}

/**
 * emptyIfNull(List<T> list)
 * Returns an immutable empty list if the argument is null, or the argument itself otherwise.
 * 返回一个不可改变的空列表
 */
@Test
public void test2() {
    // case 1: result = [zheng, hui, xiang]
    List<String> list = Arrays.asList("zheng", "hui", "xiang");
    List<String> result = ListUtils.emptyIfNull(list);
    System.out.println("result = " + result);

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

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

    // case 4: java.lang.UnsupportedOperationException
    result.add("name");
    System.out.println("result = " + result);
}

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

    // case 2: java.lang.UnsupportedOperationException: List is fixed size
//        result.add("newEle");
//        System.out.println("result = " + result);

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

    // case 4:
    result.set(2, "xiang2");
    System.out.println("result = " + result); // result = [zheng, hui, xiang2]
}

/**
 * hashCodeForList(Collection<?> list)
 * Generates a hash code using the algorithm specified in List.hashCode().
 */
@Test
public void test4() {
    List<String> list = Arrays.asList("zheng", "hui", "xiang");
    int hashCode = ListUtils.hashCodeForList(list);
    System.out.println("hashCode = " + hashCode); // hashCode = -202326244
}


/**
 * 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() {
    List<String> list = Arrays.asList("zheng", "hui", "xiang");
    Predicate<String> p = e -> e.length() == 3;
    int index = ListUtils.indexOf(list, p);
    System.out.println("index = " + index); // index = 1
}

/**
 * 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() {
    List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> list2 = Arrays.asList(3, 4, 5, 6, 7);
    List<Integer> result = ListUtils.intersection(list1, list2);
    System.out.println("result = " + result); // result = [3, 4, 5]
}

/**
 * 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() {
    // case 1:
    List<String> list1 = Arrays.asList("1", "2", "3");
    List<String> list2 = Arrays.asList("1", "2", "3");
    boolean equals = ListUtils.isEqualList(list1, list2);
    System.out.println("equals = " + equals); // equals = true

    // case 2:
    list2 = Arrays.asList("2", "1", "3");
    equals = ListUtils.isEqualList(list1, list2);
    System.out.println("equals = " + equals); // equals = false
}

/**
 * lazyList(List<E> list, Factory<? extends E> factory)
 * Returns a "lazy" list whose elements will be created on demand.
 */
@Test
public void test8() {
    long beginTime = System.nanoTime();
    List<String> list = new ArrayList<>();
    list.add("zheng");
    list.add("hui");
    list.add("xiang");
    Factory<String> factory = () -> {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        int length = 1 + random.nextInt(5);
        for (int i = 0; i < length; i++) {
            // 随机生成字母
            char letter = (char) (97 + random.nextInt(26));
            sb.append(letter);
        }
        return sb.toString();
    };
    List<String> result = ListUtils.lazyList(list, factory);
    System.out.println("result = " + result); // result = [zheng, hui, xiang]

    int beginIndex = result.size();
    for (int i = beginIndex; i < 10; i++) {
        String ele = result.get(i);
        String temp = String.format("ele_%d = %s", i, ele);
        System.out.println("temp = " + temp);
        /**
         * temp = ele_3 = yuumd
         * temp = ele_4 = rtyi
         * temp = ele_5 = vhtem
         * temp = ele_6 = c
         * temp = ele_7 = u
         * temp = ele_8 = cexjj
         * temp = ele_9 = y
         */
    }
    long endTime = System.nanoTime();
    System.out.printf("cost time is %f", (endTime - beginTime) / 1000000000.0); // cost time is 0.108586
}

/**
 * lazyList(List<E> list, Transformer<Integer,? extends E> transformer)
 * Returns a "lazy" list whose elements will be created on demand.
 */
@Test
public void test9() {
    List<String> list = new ArrayList<>();
    list.add("zheng");
    list.add("hui");
    list.add("xiang");
    Transformer<Integer, String> transformer = input -> input.toString() + "-index";
    List<String> result = ListUtils.lazyList(list, transformer);
    System.out.println("result = " + result); // result = [zheng, hui, xiang]

    String aa = result.get(5);
    System.out.println("aa = " + aa); // aa = 5-index
    System.out.println("result = " + result); // result = [zheng, hui, xiang, null, null, 5-index]
}

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

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

/**
 * longestCommonSubsequence(List<E> a, List<E> b, Equator<? super E> equator)
 * Returns the longest common subsequence (LCS) of two sequences (lists).
 */
@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<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<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);
    }
    
    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<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<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)
 * 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)
 * 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<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)
 * 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<E> list)
 * Returns a synchronized list backed by the given list.
 */
@Test
public void test21() {
    List<String> list = Arrays.asList("zheng", "hui", "xiang");
    List<String> result = ListUtils.synchronizedList(list);
    System.out.println("result = " + result); // result = [zheng, hui, xiang]
}

/**
 * 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 = {"zheng", "hui", "xiang"};
    CollectionUtils.addAll(list, arr);

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

/**
 * 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)
 * 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
}

apache公共集合类用法探究之ListUtils-优快云博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值