CollectionUtils工具类之并集union(arr1,arr2)和差集subtract(arr1,arr2)
一、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)的更多相关文章
- java代码之美(12)---CollectionUtils工具类
java代码之美(12)---CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的, 而不是springframework下的CollectionUt ...
- CollectionUtils工具类
CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的,可以使代码更加简洁和安全. 使用前需导入依赖 <dependency> <gr ...
- java代码(12) ---CollectionUtils工具类
CollectionUtils工具类 CollectionUtils工具类是在apache下的,而不是springframework下的CollectionUtils 个人觉得在真实项目中Collec ...
- StringUtils、CollectionUtils工具类的常用方法
唯能极于情,故能极于剑 欢迎来到 “程序牛CodeCow” 的博客,有问题请及时关注小编公众号 “CodeCow”,大家一起学习交流 下面将为大家演示StringUtils.CollectionUti ...
- 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空
通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...
- CollectionUtils工具类的常用方法
集合判断: 例1: 判断集合是否为空: CollectionUtils.isEmpty(null): true CollectionUtils.isEmpty(new ArrayList()): t ...
- CollectionUtils工具类中常用方法
@SuppressWarnings("rawtypes") @Test public void test1() { List<String> coll = new Ar ...
- CollectionUtils工具类使用指南
CollectionUtils提供很多对集合的操作方法,常用的方法如下:(参考文章:http://www.open-open.com/code/view/1420470842125) import o ...
- 集合工具类CollectionUtils、ListUtils、SetUtils、MapUtils的使用
主要用它的isEmpty(final Collection<?> coll)静态方法来判断一个给定的集合是否为null或者是否长度为0.最近才发现此工具类还可以取集合的交集.并集.甚至差集 ...
随机推荐
- Deep Upsupervised Cardinality Estimation 解读(2019 VLDB)
Deep Upsupervised Cardinality Estimation 本篇博客是对Deep Upsupervised Cardinality Estimation的解读,原文连接为:htt ...
- MySQL 8.0.25 MSI Install 安装过程
官网下载地址: https://dev.mysql.com/downloads/mysql 其中web-community需要联网安装,另外一个可以离线安装.我下载的是离线安装包. 1.双击安 ...
- shell脚本的调试sh-x
转至:https://blog.csdn.net/yjgithub/article/details/80908079 目录 一.简介 二.sh -x 脚本名.sh 三.set -x 一.简介 使用sh ...
- 论文解读(GIN)《How Powerful are Graph Neural Networks》
Paper Information Title:<How Powerful are Graph Neural Networks?>Authors:Keyulu Xu, Weihua Hu, ...
- (第二章第一部分)TensorFlow框架之文件读取流程
本章概述:在第一章的系列文章中介绍了tf框架的基本用法,从本章开始,介绍与tf框架相关的数据读取和写入的方法,并会在最后,用基础的神经网络,实现经典的Mnist手写数字识别. 有四种获取数据到Tens ...
- 在java的继承中你是否有这样的疑惑?
一.问题 最近在写代码,有两个属性很相近的类,其中80%的属性(字段)都是一样的,剩下的才是各自不一样的,在设计的时候,采用了继承的方式,抽象除了一个父类,大概如下, 有FirstChild和Seco ...
- laravel 终端命令
创建模块及控制器
- 微信退款通知,退款回调数据解密.SHA256签名AEAD_AES_256_GCM解密
$xmlResult = file_get_contents("php://input");//获取微信的数据$result = $this->xmlToArray($xml ...
- 宇宙最強的IDE - Visual Studio 25岁生日快乐
每位开发者从入门开始或多或少都会接触过 Visual Studio , 现今的 Visual Studio 除了支持传统的 C++ , C# , Visual Basic.NET ,F# 的编程语言外 ...
- 云原生入门 第五章:kubernetes学习实践
1. 简介 在本章中,我们将学习不同的Kubernetes对象,它们的用途以及如何与它们交互. 在设置集群或使用现有集群之后,我们可以开始部署一些工作负载.Kubernetes中最小的计算单元不是一个 ...