学习集合工具类CollectionUtils——List对象案例

  • 一、依赖
  • 二、案例
  • 三、结果展示

一、依赖

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

二、案例

    @Testvoid test17() {People[] peopleArray = new People[2];for (int i = 0; i < peopleArray.length; i++) {peopleArray[i] = new People(i, String.valueOf(i), i);}List<People> peopleList1 = Lists.newArrayList();peopleList1.add(new People(1, "小李", 3));peopleList1.add(new People(2, "小张", 2));List<People> peopleList2 = Lists.newArrayList();peopleList2.add(new People(1, "小李", 3));peopleList2.add(new People(3, "小皇", 1));List<People> peopleList3 = Lists.newArrayList();log.info("判断集合是否为空,只对Collection及子类有效");boolean isEmpty = CollectionUtils.isEmpty(peopleList1);log.info(String.valueOf(isEmpty));log.info("判断集合是否不为空,只对Collection及子类有效");boolean isNotEmpty = CollectionUtils.isNotEmpty(peopleList1);log.info(String.valueOf(isNotEmpty));log.info("如果参数2处不为null,把参数2添加到peopleList3集合中");boolean AddIgnoreNull = CollectionUtils.addIgnoreNull(peopleList3, new People(4, "小蓝", 0));log.info(String.valueOf(AddIgnoreNull));log.info(peopleList3.toString());log.info("从peopleList1中删除peopleList2");List<People> removeAll = CollectionUtils.removeAll(peopleList1, peopleList2).stream().collect(Collectors.toList());log.info(removeAll.toString());log.info("获取并集");List<People> union = CollectionUtils.union(peopleList1, peopleList2).stream().collect(Collectors.toList());log.info(union.toString());log.info("获取交集");List<People> intersection = CollectionUtils.intersection(peopleList1, peopleList2).stream().collect(Collectors.toList());log.info(intersection.toString());log.info("获取交集的补集");List<People> disjunction = CollectionUtils.disjunction(peopleList1, peopleList2).stream().collect(Collectors.toList());log.info(disjunction.toString());log.info("获取差集");List<People> subtract = CollectionUtils.subtract(peopleList1, peopleList2).stream().collect(Collectors.toList());log.info(subtract.toString());log.info("返回peopleList2在peopleList1中的数据");List<People> retainAll = CollectionUtils.retainAll(peopleList1, peopleList2).stream().collect(Collectors.toList());log.info(retainAll.toString());log.info("数组反转");CollectionUtils.reverseArray(peopleArray);log.info(Arrays.stream(peopleArray).collect(Collectors.toList()).toString());log.info("返回每个元素出现的个数");Map<People, Integer> cardinalityMap = CollectionUtils.getCardinalityMap(peopleList1);log.info(cardinalityMap.toString());log.info("返回对象在集合中出现的次数");int cardinality = CollectionUtils.cardinality(new People(1, "小李", 3), peopleList1);log.info(String.valueOf(cardinality));log.info("返回结合指定位置的数");People getPeople = CollectionUtils.get(peopleList1, 0);log.info(getPeople.toString());log.info("判断两个集合是否相等");boolean equalCollection = CollectionUtils.isEqualCollection(peopleList1, peopleList2);log.info(String.valueOf(equalCollection));log.info("判断集合1是否小于集合2");boolean properSubCollection = CollectionUtils.isProperSubCollection(peopleList1, peopleList2);log.info(String.valueOf(properSubCollection));log.info("判断是否是子集");boolean subCollection = CollectionUtils.isSubCollection(peopleList1, peopleList2);log.info(String.valueOf(subCollection));log.info("判断是否存在交集");boolean containsAny = CollectionUtils.containsAny(peopleList1, peopleList2);log.info(String.valueOf(containsAny));log.info("根据条件筛选集合元素");List<People> collect = CollectionUtils.select(peopleList1, new Predicate<People>() {@Overridepublic boolean evaluate(People people) {if (people.getId() % 2 == 0) {return true;}return false;}}).stream().collect(Collectors.toList());log.info(collect.toString());log.info("根据指定方法处理集合元素,类似List的map()");CollectionUtils.transform(peopleList1, new Transformer<People, People>() {@Overridepublic People transform(People people) {people.setName(people.getName() + "_update");return people;}});log.info(peopleList1.toString());log.info("基本和select一样");People findPeople = CollectionUtils.find(peopleList1, new Predicate<People>() {@Overridepublic boolean evaluate(People people) {if (people.getId() % 2 != 0) {return true;}return false;}});log.info(findPeople.toString());log.info("调用每个元素的指定方法");CollectionUtils.forAllDo(peopleList1, new Closure<People>() {@Overridepublic void execute(People people) {people.setName(people.getName() + "_update");}});log.info(peopleList1.toString());}

三、结果展示

判断集合是否为空,只对Collection及子类有效false判断集合是否不为空,只对Collection及子类有效true如果参数2处不为null,把参数2添加到peopleList3集合中true[People(id=4, name=小蓝, jgId=0)]从peopleList1中删除peopleList2[People(id=2, name=小张, jgId=2)]获取并集[People(id=1, name=小李, jgId=3), People(id=2, name=小张, jgId=2), People(id=3, name=小皇, jgId=1)]获取交集[People(id=1, name=小李, jgId=3)]获取交集的补集[People(id=2, name=小张, jgId=2), People(id=3, name=小皇, jgId=1)]获取差集[People(id=2, name=小张, jgId=2)]返回peopleList2在peopleList1中的数据[People(id=1, name=小李, jgId=3)]数组反转[People(id=1, name=1, jgId=1), People(id=0, name=0, jgId=0)]返回每个元素出现的个数{People(id=1, name=小李, jgId=3)=1, People(id=2, name=小张, jgId=2)=1}返回对象在集合中出现的次数1返回结合指定位置的数People(id=1, name=小李, jgId=3)判断两个集合是否相等false判断集合1是否小于集合2false判断是否是子集false判断是否存在交集true根据条件筛选集合元素[People(id=2, name=小张, jgId=2)]根据指定方法处理集合元素,类似List的map()[People(id=1, name=小李_update, jgId=3), People(id=2, name=小张_update, jgId=2)]基本和select一样People(id=1, name=小李_update, jgId=3)调用每个元素的指定方法[People(id=1, name=小李_update_update, jgId=3), People(id=2, name=小张_update_update, jgId=2)]
  • 即使慢也要驰而不息。
  • 为梦想追逐,随时可以上路。
  • 走上坡的时候要对别人好一点,因为你走下坡的时候会碰到他。
  • 宁愿失败地做你爱做的事情,也不要成功地做你恨做的事情。
  • 即使走的慢也决不后退。
  • 有人像家雀儿不愿意挪窝,有人像候鸟永远在路上。
  • 用自己的双脚在这土地上占据一个位置,无论是踩在泥泞里还是荆棘上。
  • 不说什么强强联手,不谈什么1+1> 2。
  • 不是为了已经改写的历史。
  • 更不是为了将要开创的未来。
  • 唯一值得庆祝的。
  • 不是纸上的两个签名,而是找到一个千杯少的知己。
  • 我才华横溢,前途光明,有着足以改变世界的想法。
  • 投资我一定是您最一本万利的投资,这是您当年说的。
  • 我的一切努力都是为了兑现它。
  • 贵人,就是那个我不想令他失望的人。
  • 不问初心,方得始终。

学习集合工具类CollectionUtils——List对象案例相关推荐

  1. 【集合工具类:Collections】

    集合工具类:Collections (1) 是针对集合进行操作的工具类 (2) 面试题:Collection 和 Collections 的区别 A:Collection 是单列集合的顶层接口,有两个 ...

  2. java list排序工具类_java 之 Collections集合工具类排序

    数组有工具类Arrays,集合也有一个工具类Collections. sort方法: sort(List list):根据其元素natural ordering对制定的列表进行排序 sort(List ...

  3. Java集合篇:Map接口、Map接口的实现类、Collections集合工具类

    目录 一.Map接口 1.1 Map接口概述 1.2 Map接口常用功能 二.Map接口的实现类 2.1 Map实现类之一:HashMap 2.1.1 HashMap概述 2.1.2 HashMap的 ...

  4. 【小白学Java】D26 》》》Collections集合工具类

    [友情链接]---–->Java中的各种集合大汇总,学习整理 [友情链接]----–> collection集合 [友情链接]----–> ArrayList集合及其常用功能 [友情 ...

  5. 字符串工具类、数组工具类、集合工具类、转型操作工具类、编码与解码操作工具类...

    package hjp.smart4j.framework.util;import org.apache.commons.lang3.StringUtils;/*** 字符串工具类*/ public ...

  6. [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具

    原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.G ...

  7. Collections集合工具类的方法_sort(List,Comparator)

    简述Comparable和Comparator两个接口的区别. Comparable:强行对实现它的每个类的对象进行整体排序.这种排序被称为类的自然排序,类的compareTo方法被称为它的自然比较方 ...

  8. Collections集合工具类的方法_sort(List)

    Comparator比较器 我们还是先研究这个方法 public static <T> void sort(List<T> list):将集合中元素按照默认规则排序. 不过这次 ...

  9. SAP ABAP里存在Java List这种集合工具类么?CL_OBJECT_COLLECTION了解一下

    Jerry以前在工作中交替做着ABAP和Java开发时,总是在使用一种语言时,怀念另一种语言的便利之处,比如用ABAP开发时,怀念Java里以List为代表的功能强大,使用方便的集合工具类. List ...

最新文章

  1. linux离线安装g 报错,Linux离线安装mysql 5.6详细步骤
  2. Erlang--热更新
  3. 增强学习(Reinforcement Learning and Control)
  4. mysql command line client和mysql.exe输入密码后闪退问题解决方法
  5. Java学习笔记——封装
  6. 读取csv绘制直方图_[python]用tushare接口绘制Bollinger Bands
  7. 2017西安交大ACM小学期数论 [阅兵式]
  8. 【数据库系统】SQL查询的注意事项
  9. 模型法在评级中的应用
  10. Python MapReduce
  11. 教你手写Java层handler机制
  12. js alert追加html,利用JQ来美化Js的alert弹出框
  13. 从小锁匠铺到工业造纸巨头,这家德国百年家族企业不断改写世界工程技术史 | 能动观察...
  14. RNA甲基化修饰种类
  15. 项目管理计划怎么写?这9大步骤要知道
  16. 『HDU 5855』Less Time, More profit
  17. ABAP 关于 delete adjacent duplicates from的小心得
  18. linux rm 回收站,给rm设置回收站
  19. 雅马哈机器人左手右手系统_雅马哈四轴机器人调试笔记
  20. C语言计算5+55+555+5555+55555

热门文章

  1. WPF入门教程(八)--依赖属性(4)
  2. 【mysql学习笔记】窗口函数汇总
  3. 1396:病毒(virus)
  4. (五)EDCA机制详解
  5. hammer.js移动端拖拽缩放旋转元素
  6. .net xml ajax 注册 ashx,AJAX.NET安装配置全指南
  7. 写代码必备神器!腕上潮流敲代码贼爽!
  8. 圣诞老人-jq-html
  9. Git:git-rev-parse 命令学习
  10. YOLOV5代码理解——类权重系数和图像权重系数