Java猿社区—Apache Commons Collections—CollectionUtils工具类详解

欢迎关注作者博客
简书传送门

 

文章目录

 

前言

阅读源码的重要性,后期会对各大开源框架相关源码做详细阅读,并熟悉使用,本次主要对Apache Commons Collections中CollectionUtils类进行示例分析,如有错误,请多指教。


通过apache-commons包中的org.apache.commons.collections.CollectionUtils集合操作工具类 对集合间进行合并union、交叉intersection、分离disjunction、减去subtract、任意包含containsAny、判断是否为子集isSubCollection、颠倒序列reverseArray及判断是否填满isFull等操作。

代码示例

package com.zzx.apache.commons.chapter1;

import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * <p>
 * 通过apache-commons包中的org.apache.commons.collections.CollectionUtils集合操作工具类 对集合间进行合并union、交叉intersection、分离disjunction、减去subtract、任意包含containsAny、
 * 判断是否为子集isSubCollection、颠倒序列reverseArray及判断是否填满isFull等操作。
 * </p>
 **/
public class CollectionsUtilsDemo {

    /** Arrays.asList()返回的是Arrays内部类ArraysList,不可对其进行add、remove等操作,返回报UnsupportedOperationException */
    /** java.util.ArrayList和Arrays内部类ArraysList都继承AbstractList,remove、add等方法AbstractList中是默认throw UnsupportedOperationException而且不作任何操作。*/
    /** java.util.ArrayList重新了这些方法而Arrays的内部类ArrayList没有重新,所以会抛出异常。*/
    // @Deprecated
    // private static List<String> list1 = Arrays.asList(new String[] {"1", "2", "3", "1"});
    // @Deprecated
    // private static List<String> list2 = Arrays.asList(new String[] {"2", "3", "4"});
    // @Deprecated
    // private static List<String> list3 = Arrays.asList(new String[] {"1", "2"});
    /** 解决 */
    private static List<String> list1 = new ArrayList<>(Arrays.asList(new String[] {"1", "2", "3", "1", "5"}));
    private static List<String> list2 = new ArrayList<>(Arrays.asList(new String[] {"2", "3", "1"}));
    private static List<String> list3 = new ArrayList<>(Arrays.asList(new String[] {"1", "2"}));

    @Data
    class Employee {
        private String name;
        private String email;
        private int age;
        private double salary;
        /** 是否在职 */
        private boolean activeEmployee;

        public Employee(String name, String email, int age, double salary, boolean activeEmployee) {
            this.name = name;
            this.email = email;
            this.age = age;
            this.salary = salary;
            this.activeEmployee = activeEmployee;
        }
    }

    /**
     * 判断两个集合是否和相同元素
     */
    public void containsAnyT1() {
        // 判断两个集合是否和相同元素
        boolean b = CollectionUtils.containsAny(list1, list2);
        System.out.println(b);
    }

    /**
     * 得到两个集合中相同的元素
     */
    @Test
    public void intersectionT1() {
        Collection b = CollectionUtils.intersection(list1, list2);
        System.out.println(b);
    }

    /**
     * 合并两个集合,不去重
     */
    @Test
    public void unionT1() {
        Collection b = CollectionUtils.union(list1, list2);
        System.out.println(b);
    }

    /**
     * 取两个集合差集,不去重
     */
    @Test
    public void disjunctionT1() {
        Collection b = CollectionUtils.disjunction(list1, list2);
        System.out.println(b);
    }

    /**
     * list1 - list2 = 剩余元素组成的集合
     */
    @Test
    public void subtractT1() {
        // Collection b = CollectionUtils.subtract(list1, list2);
        Collection b = CollectionUtils.subtract(list2, list1);
        System.out.println(b);
    }

    /**
     * 统计集合中各元素出现的次数,并Map<Object, Integer>输出
     */
    @Test
    public void getCardinalityMapT1() {
        Map cardinalityMap = CollectionUtils.getCardinalityMap(list1);
        cardinalityMap.forEach((k, v) -> System.out.println(k + ":" + v));
    }

    /**
     * a是否b集合子集,a集合大小<=b集合大小
     */
    @Test
    public void isSubCollectionT1() {
        // boolean subCollection = CollectionUtils.isSubCollection(list3, list1);
        boolean subCollection = CollectionUtils.isSubCollection(list3, list2);
        System.out.println(subCollection);
    }

    /**
     * a是否b集合子集,a集合大小<b集合大小
     */
    @Test
    public void isProperSubCollectionT1() {
        // boolean subCollection = CollectionUtils.isSubCollection(list3, list1);
        boolean subCollection = CollectionUtils.isProperSubCollection(list3, list2);
        System.out.println(subCollection);
    }

    /**
     * 两个集合是否相同
     */
    @Test
    public void isEqualCollectionT1() {
        boolean subCollection = CollectionUtils.isSubCollection(list1, list1);
        // boolean subCollection = CollectionUtils.isEqualCollection(list3, list2);
        System.out.println(subCollection);
    }

    /**
     * 某元素在集合中出现的次数
     */
    @Test
    public void cardinalityT1() {
        int cardinality = CollectionUtils.cardinality("1", list1);
        System.out.println(cardinality);
    }

    /**
     * 返回集合中满足函数式的唯一元素,只返回最先处理符合条件的唯一元素
     */
    @Test
    public void findT1() {
        Object o = CollectionUtils.find(list1, e -> Integer.parseInt(e.toString()) > 2);
        System.out.println(o.toString());
    }

    /**
     * 对集合中的对象中的某一属性进行批量更新,closure为需要更新的属性对象
     * 如对集合中所有员工的加薪20%
     */
    @Test
    public void forAllDoT1() {
        // // create the closure
        // List<Employee> employees = new ArrayList<>();
        // Employee e1 = new Employee("e1", "e1.com", 21, 10000, true);
        // Employee e2 = new Employee("e2", "e2.com", 22, 14000, false);
        // Employee e3 = new Employee("e3", "e3.com", 23, 12000, true);
        // Employee e4 = new Employee("e4", "e4.com", 21, 12000, true);
        // Closure<E> closure = new Closure() {
        //     @Override
        //     public void execute(Employee e) {
        //         e.setSalary(e.getSalary() * 1.2);
        //     }
        // };

    }

    /**
     * 过滤集合中满足函数式的所有元素
     */
    @Test
    public void filterT1() {
        CollectionUtils.filter(list1, e -> Integer.parseInt(e.toString()) > 1);
        list1.forEach(s -> {
            System.out.println(s);
        });
    }

    /**
     * 转换新的集合,对集合中元素进行操作,如每个元素都累加1
     */
    @Test
    public void transformT1() {

        CollectionUtils.transform(list1, new Transformer() {
            @Override
            public Object transform(Object o) {
                Integer num = Integer.parseInt((String)o);
                return String.valueOf(++num);
            }
        });
        list1.forEach(s -> {
            System.out.println(s);
        });

        System.out.println("============================");

        // JDK8
        List<String> temp = new ArrayList<>();
        list1.stream().forEach(i -> {
            int num = Integer.parseInt(i);
            temp.add(String.valueOf(num));
        });
        temp.forEach(System.out::println);
    }

    /**
     * 返回集合中满足函数式的数量
     */
    @Test
    public void countMatchesT1() {
        int num = CollectionUtils.countMatches(list1, i -> Integer.parseInt((String)i) > 0);
        System.out.println(num);
    }

    /**
     * 将满足表达式的元素存入新集合中并返回新集合元素对象
     */
    @Test
    public void selectT1() {
        Collection select = CollectionUtils.select(list1, i -> Integer.parseInt((String)i) > 2);
        select.forEach(System.out::println);
    }

    /**
     * 将不满足表达式的元素存入新集合中并返回新集合元素对象
     */
    @Test
    public void selectRejectedT1() {
        Collection select = CollectionUtils.selectRejected(list1, i -> Integer.parseInt((String)i) > 2);
        select.forEach(System.out::println);
    }

    /**
     * collect底层调用的transform方法
     * 将所有元素进行处理,并返回新的集合
     */
    @Test
    public void collectT1() {
        Collection collecttion = CollectionUtils.collect(list1, new Transformer() {
            @Override
            public Object transform(Object o) {
                int i = Integer.parseInt((String)o);
                return ++i;
            }
        });
        collecttion.forEach(System.out::println);
    }

    /**
     * 将一个数组或集合中的元素全部添加到另一个集合中
     */
    @Test
    public void adAllT1() {
        CollectionUtils.addAll(list1, new String[]{"5", "6"});
        CollectionUtils.addAll(list1, list2.toArray());
        list1.forEach(System.out::println);
    }

    /**
     * 返回集合中指定下标元素
     */
    @Test
    public void indexT1() {
        String index = (String)CollectionUtils.index(list1, 2);
        System.out.println(index);
    }

    /**
     * 返回集合中指定下标元素
     */
    @Test
    public void getT1() {
        String index = (String)CollectionUtils.get(list1, 2);
        System.out.println(index);
    }

    /**
     * 判断集合是否为空
     */
    @Test
    public void isEmptyT1() {
        int[] arr = new int[5];
        arr[0] = 1;
        arr[1] = 1;
        arr[2] = 1;
        arr[3] = 1;
        arr[4] = 1;
        boolean empty = CollectionUtils.isFull(new ArrayList(Arrays.asList(arr)));
        System.out.println(empty);
    }

    /**
     * 判断集合是否为空
     */
    @Test
    public void isFullT1() {
        boolean full = CollectionUtils.isFull(list1);
        System.out.println(full);
    }

    /**
     * 返回集合最大空间
     */
    @Test
    public void maxSizeT1() {
        // List<Integer> boundedList = new ArrayList<>(8);
        // int i = CollectionUtils.maxSize(boundedList);
        // System.out.println(i);
    }

    /**
     * 只要集合中元素不满足表达式就抛出异常
     */
    @Test
    public void predicatedCollectionT1() {
        Collection collection = CollectionUtils.predicatedCollection(list1, i -> Integer.parseInt((String)i) > 1);
        collection.forEach(System.out::println);
    }

    /**
     * 只要集合中元素不满足表达式就抛出异常
     */
    @Test
    public void removeAllT1() {
        boolean b = list1.removeAll(list2);
        System.out.println(b);
        list1.forEach(System.out::println);
    }

    /**
     * 只要集合中元素不满足表达式就抛出异常
     */
    @Test
    public void synchronizedCollectionT1() {
        Collection collection = CollectionUtils.synchronizedCollection(list1);
        collection.forEach(System.out::println);
    }

    /**
     * 只要集合中元素不满足表达式就抛出异常
     */
    @Test
    public void unmodifiedCollectionT1() {
        Collection collection = CollectionUtils.unmodifiableCollection(list1);
        collection.forEach(System.out::println);
    }

    /**
     * 只要集合中元素不满足表达式就抛出异常
     */
    @Test
    public void predicatedCollectionT2() {
        // Collection collection = CollectionUtils.predicatedCollection(list1, i -> Integer.parseInt((String)i) > 0);
        // Collection collection = CollectionUtils.typedCollection(list1, String.class);
        Collection collection = CollectionUtils.transformedCollection(list1, new Transformer() {
            @Override
            public Object transform(Object o) {
                int n = Integer.parseInt((String)o);
                return n + n;
            }
        });
        collection.forEach(System.out::println);
    }

}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374

欢迎加入Java猿社区!

免费领取我历年收集的所有学习资料哦!

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值