一、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. 无法打开备份设备,出现操作系统错误 5(拒绝访问)(sql server备份)

    问题:无法打开备份设备 .出现操作系统错误 5(拒绝访问) 原因:你用sqlserver进行备份的时候,必须要是完整的路径,操作sqlserver的和磁盘有关的路径都必须要到文件名这一层. 解决方案: ...

  2. Linux命令(ping-telnet-netstat-curl-ps)

    转至:https://www.jianshu.com/p/577bbd15a6f8 1.ping命令 ping命令用来测试主机之间网络的连通性.执行ping指令会使用ICMP传输协议,发出要求回应的信 ...

  3. AcWing 325. 计算机

    传送门 题目大意: 一棵无根树,每条边有一个距离,求每个顶点到距离其最远的顶点的距离. 思路: 考虑树形DP+换根. 令D[x]x到以x为根的子树当中的最长距离,d[x]为次长距离,U[x]为x向上走 ...

  4. 报错:net::err_unknown_url_scheme的解决办法

    在项目中设置了api请求和web页面请求的地址,如下图: 控制台报错,如下图: 问题是:没有加入"http://"这个头,因此访问不到. 解决办法: 再次访问正常

  5. Go select 死锁引发的思考

    Go select 死锁引发的思考 https://mp.weixin.qq.com/s/Ov1FvLsLfSaY8GNzfjfMbg一文引发的延续思考 上文总结 总结一 package main i ...

  6. 修饰符-final

    Java是由C/C++泛生的,其也保留了C/C++的部分特性,如关键字.在C/C++中,关键字有着特殊的含义. final修饰符 在编程中,一般会存在一些变量或方法,程序员不让其数据"发生改 ...

  7. petite-vue源码剖析-事件绑定`v-on`的工作原理

    在书写petite-vue和Vue最舒服的莫过于通过@click绑定事件,而且在移除元素时框架会帮我们自动解除绑定.省去了过去通过jQuery的累赘.而事件绑定在petite-vue中就是一个指令(d ...

  8. ElasticSearch常用查询命令-kibana中使用

    目录 初学ES 只创建索引(表) 1. 创建 2.创建好后查看索引结构 添加文档(数据) 查看文档(数据) 修改文档数据(数据update) put方式修改 post方式修改 删除文档&索引 ...

  9. 微信小程序获取当前的时间戳

    js文件中进行计算var timestamp = Date.parse(new Date()); timestamp = timestamp / 1000; console.log(timestamp ...

  10. Forbidden You don't have permission to access this resource. 解决办法!

    这两天在使用hmailserver+roundcubemail 搭建邮箱时遇到的一些坑和大家分享一下,避免少踩坑. 关用httpd.conf及httpd-vhosts.conf配置我贴出来供大家参考. ...