一、CollectionUtils工具类之并集union(arr1,arr2)和差集subtract(arr1,arr2)

采用的类:

import org.apache.commons.collections4.CollectionUtils;

①并集union(arr1,arr2)

这是将两个集合加在一起,然后去重

List<Integer> orderList1 = Arrays.asList(1, 2, 3);

List<Integer> orderList2 = Arrays.asList(3, 4, 5);

List<Integer> union = new ArrayList<>(CollectionUtils.union(orderList1, orderList2)); 

// 1,2,3,4,5
System.out.println("union = " + union);

②差集subtract(arr1,arr2)

这是将两个集合的差,如1,2,3 差集3,4,5就会得到1,2,将3这个重复的去掉

List<Integer> orderList1 = Arrays.asList(1, 2, 3);

List<Integer> orderList2 = Arrays.asList(3, 4, 5);

List<Integer> subtract = new ArrayList<>(CollectionUtils.subtract (orderList1, orderList2));

// 1,2
System.out.println("subtract = " + subtract );

③遇到的问题

返回值是父级的Collection<O>,这样的话如果只想做合并去重的话就会导致类型不一致,而照成麻烦

List<Integer> orderList1 = Arrays.asList(1, 2, 3);

List<Integer> orderList2 = Arrays.asList(3, 4, 5);

Collection<Integer> union = CollectionUtils.union(orderList1, orderList2);

// 1,2,3,4,5
System.out.println("union = " + union);

如需要转换为对应的类型,如上转回List<Integer>可以有几种方案

方案1

List<Integer> union = new ArrayList<>(CollectionUtils.union(orderList1, orderList2));

方案2

// 这个会警告,我们这里是加了一个.distinct()做过度
List<Integer> union1 = CollectionUtils.union(orderList1, orderList2).stream().collect(Collectors.toList());

CollectionUtils工具类之并集union(arr1,arr2)和差集subtract(arr1,arr2)的更多相关文章

  1. java代码之美(12)---CollectionUtils工具类

    java代码之美(12)---CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的, 而不是springframework下的CollectionUt ...

  2. CollectionUtils工具类

    CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的,可以使代码更加简洁和安全. 使用前需导入依赖 <dependency> <gr ...

  3. java代码(12) ---CollectionUtils工具类

    CollectionUtils工具类 CollectionUtils工具类是在apache下的,而不是springframework下的CollectionUtils 个人觉得在真实项目中Collec ...

  4. StringUtils、CollectionUtils工具类的常用方法

    唯能极于情,故能极于剑 欢迎来到 “程序牛CodeCow” 的博客,有问题请及时关注小编公众号 “CodeCow”,大家一起学习交流 下面将为大家演示StringUtils.CollectionUti ...

  5. 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空

    通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...

  6. CollectionUtils工具类的常用方法

    集合判断:  例1: 判断集合是否为空: CollectionUtils.isEmpty(null): true CollectionUtils.isEmpty(new ArrayList()): t ...

  7. CollectionUtils工具类中常用方法

    @SuppressWarnings("rawtypes") @Test public void test1() { List<String> coll = new Ar ...

  8. CollectionUtils工具类使用指南

    CollectionUtils提供很多对集合的操作方法,常用的方法如下:(参考文章:http://www.open-open.com/code/view/1420470842125) import o ...

  9. 集合工具类CollectionUtils、ListUtils、SetUtils、MapUtils的使用

    主要用它的isEmpty(final Collection<?> coll)静态方法来判断一个给定的集合是否为null或者是否长度为0.最近才发现此工具类还可以取集合的交集.并集.甚至差集 ...

随机推荐

  1. 【C#反射】BindingFlags 枚举

    BindingFlags 枚举用途:Type的类方法中,用于筛选成员. type.InvokeMember方法中 type.GetConstructor 方法中 type.GetFiles方法中 ty ...

  2. 【C#反射】开篇

    微软官方教程:https://docs.microsoft.com/zh-cn/dotnet/framework/reflection-and-codedom/viewing-type-informa ...

  3. linux中docker容器安装vi命令详解

    在使用docker容器时,同时你docker里的系统正好是debian或ubuntu的时候,有时候里边没有安装vim,敲vim命令时提示说:vim: command not found,这个时候就需要 ...

  4. python爬虫之抓取小说(逆天邪神)

    2022-03-06 23:05:11 申明:自我娱乐,对自我学习过程的总结. 正文: 环境: 系统:win10, python版本:python3.10.2, 工具:pycharm. 项目目标: 实 ...

  5. webrtc学习笔记积累

    https://blog.csdn.net/xyphf/article/details/106536584

  6. appium ios 相对坐标点击和控件滑动操作

    环境: 系统:ios 10.13.6 (17G12034) appium:1.14.0 xcode:10.1 iphone:iphone7 12.4 在尝试使用driver.tap([(a,b)],5 ...

  7. 分布式 PostgreSQL 集群(Citus)官方示例 - 实时仪表盘

    Citus 提供对大型数据集的实时查询.我们在 Citus 常见的一项工作负载涉及为事件数据的实时仪表板提供支持. 例如,您可以是帮助其他企业监控其 HTTP 流量的云服务提供商.每次您的一个客户端收 ...

  8. 前端面试题(js)

    js 基础面试题 css 面试题 js 面试题 JavaScript 有几种类型的值?,你能画一下他们的内存图吗 原始数据类型(Undefined,Null,Boolean,Number.String ...

  9. SpringSecurity-5.11-课堂笔记-01

  10. Mybatis的ResultMap对column和property

    首先,先看看这张图,看能不能一下看明白:   select元素有很多属性(这里说用的比较多的): id:命名空间唯一标识,可以被用来引用这条语句 parameterType:将会传入这条语句的参数类的 ...