java两个List的交集,并集
方法一:使用apache的CollectionUtils工具类(推荐)
public static void main(String[] args) {
String[] arrayA = new String[] { "1", "2", "3", "4"};
String[] arrayB = new String[] { "3", "4", "5", "6" };
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);
//1、并集 union
System.out.println(CollectionUtils.union(listA, listB));
//输出: [1, 2, 3, 4, 5, 6]
//2、交集 intersection
System.out.println(CollectionUtils.intersection(listA, listB));
//输出:[3, 4]
//3、交集的补集(析取)disjunction
System.out.println(CollectionUtils.disjunction(listA, listB));
//输出:[1, 2, 5, 6]
//4、差集(扣除)
System.out.println(CollectionUtils.subtract(listA, listB));
//输出:[1, 2]
}
方法二:List自带方法
public static void main(String[] args) {
String[] arrayA = new String[] { "1", "2", "3", "4"};
String[] arrayB = new String[] { "3", "4", "5", "6" };
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);
//1、交集
List<String> jiaoList = new ArrayList<>(listA);
jiaoList.retainAll(listB);
System.out.println(jiaoList);
//输出:[3, 4]
//2、差集
List<String> chaList = new ArrayList<>(listA);
chaList.removeAll(listB);
System.out.println(chaList);
//输出:[1, 2]
//3、并集 (先做差集再做添加所有)
List<String> bingList = new ArrayList<>(listA);
bingList.removeAll(listB); // bingList为 [1, 2]
bingList.addAll(listB); //添加[3,4,5,6]
System.out.println(bingList);
//输出:[1, 2, 3, 4, 5, 6]
}
注意 : intersection和retainAll的差别
要注意的是它们的返回类型是不一样的,intersection返回的是一个新的List集合,而retainAll返回是Bollean类型那就说明retainAll方法是对原有集合进行处理再返回原有集合,会改变原有集合中的内容。
个人观点:1、从性能角度来考虑的话,List自带会高点,因为它不用再创建新的集合。2、需要注意的是:因为retainAll因为会改变原有集合,所以该集合需要多次使用就不适合用retainAll。
注意: Arrays.asList将数组转集合不能进行add和remove操作。
原因:调用Arrays.asList()生产的List的add、remove方法时报异常,这是由Arrays.asList() 返回的市Arrays的内部类ArrayList, 而不是java.util.ArrayList。Arrays的内部类ArrayList和java.util.ArrayList都是继承AbstractList,remove、add等方法AbstractList中是默认throw UnsupportedOperationException而且不作任何操作。java.util.ArrayList重新了这些方法而Arrays的内部类ArrayList没有重新,所以会抛出异常。
所以正确做法如下
String[] array = {"1","2","3","4","5"};
List<String> list = Arrays.asList(array);
List arrList = new ArrayList(list);
arrList.add("6");
方法三:JDK1.8 stream 新特性
String[] arrayA = new String[] { "1", "2", "3", "4"};
String[] arrayB = new String[] { "3", "4", "5", "6" };
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);
// 交集
List<String> intersection = listA.stream().filter(item -> listB.contains(item)).collect(toList());
System.out.println(intersection);
//输出:[3, 4]
// 差集 (list1 - list2)
List<String> reduceList = listA.stream().filter(item -> !listB.contains(item)).collect(toList());
System.out.println(reduceList);
//输出:[1, 2]
// 并集 (新建集合:1、是因为不影响原始集合。2、Arrays.asList不能add和remove操作。
List<String> listAll = listA.parallelStream().collect(toList());
List<String> listAll2 = listB.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println(listAll);
//输出:[1, 2, 3, 4, 3, 4, 5, 6]
// 去重并集
List<String> list =new ArrayList<>(listA);
list.addAll(listB);
List<String> listAllDistinct = list.stream().distinct().collect(toList());
System.out.println(listAllDistinct);
//输出:[1, 2, 3, 4, 5, 6]
总结 : 这三种推荐第一种方式,因为第二种还需要确定该集合是否被多次调用。第三种可读性不高。
对象集合交、并、差处理
因为对象的equels比较是比较两个对象的内存地址,所以除非是同一对象,否则equel返回永远是false。
但我们实际开发中 在我们的业务系统中判断对象时有时候需要的不是一种严格意义上的相等,而是一种业务上的对象相等。在这种情况下,原生的equals方法就不能满足我们的需求了,所以这个时候我们需要重写equals方法。
说明 :String为什么可以使用equels方法为什么只要字符串相等就为true,那是因为String类重写了equal和hashCode方法,比较的是值。
public class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
/**
* 为什么重写equals方法一定要重写hashCode方法下面也会讲
*/
@Override
public int hashCode() {
String result = name + age;
return result.hashCode();
}
/**
* 重写 equals 方法 根据name和age都相同那么对象就默认相同
*/
@Override
public boolean equals(Object obj) {
Person u = (Person) obj;
return this.getName().equals(u.getName()) && (this.age.equals(u.getAge()));
}
/**
* 重写 toString 方法
*/
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
这里根据name和age都相同那么就默认相同对象。
public static void main(String[] args) {
List<Person> personList = Lists.newArrayList();
Person person1 = new Person("小小",3);
Person person2 = new Person("中中",4);
personList.add(person1);
personList.add(person2);
List<Person> person1List = Lists.newArrayList();
Person person3 = new Person("中中",4);
Person person4 = new Person("大大",5);
person1List.add(person3);
person1List.add(person4);
/**
* 1、差集
*/
System.out.println(CollectionUtils.subtract(personList, person1List));
//输出:[Person{name='小小', age=3}]
/**
* 2、并集
*/
System.out.println(CollectionUtils.union(personList, person1List));
//输出:[Person{name='小小', age=3}, Person{name='中中', age=4}, Person{name='大大', age=5}]
/**
* 3、交集
*/
System.out.println(CollectionUtils.intersection(personList, person1List));
//输出:[Person{name='中中', age=4}]
/**
* 4、交集的补集(析取)
*/
System.out.println(CollectionUtils.disjunction(personList, person1List));
//输出:[Person{name='小小', age=3}, Person{name='大大', age=5}]
}
java两个List的交集,并集相关推荐
- JAVA Hashset求集合的交集并集差集
JAVA Hashset求集合的交集并集差集 Hashset是Set接口较为常见的一个子类,该子类的最大特点是不允许保存重复的元素,并且所有的内容都采用散列(无序)的方式进行存储. package c ...
- 两个list的交集并集差集
昨天看了一本书,讲了更优雅的进行两个集合的并集.交集.差集等.比之前的那个stream流要好用. 1.并集 最正常的使用add方法. list1.addAll(list2); 注意:要是想要得到不重复 ...
- js寻找两个数组的差集_js求两个数组的交集|并集|差集|去重
let a = [1,2,3], b= [2, 4, 5]; 1.差集 (a-b 差集:属于a但不属于b的集合) a-b = [1,3] (b-a 差集:属于b但不属于a的集合) b-a = [4 ...
- [Leetcode][第题][JAVA][两个数组的交集 II1][双指针][HashMap]
[问题描述][中等] [解答思路] 1. 哈希映射 复杂度分析 class Solution {public int[] intersect(int[] nums1, int[] nums2) {if ...
- ORAClE 两个表取交集,并集,差集
这是我盗的 selct A.* from A UNION ALL/UNION/Intersect/MINUS select B.* from B; UNION ALL ---------------- ...
- oracle 并集 时间_Oracle集合运算符 交集 并集 差集
集合运算符:UNION/UNION ALL 并集,INTERSECT 交集,MINUS 差集 一.union求并集,公共部分只有包含一次 例:求emp表ename中含'A'或含有'M' SQL> ...
- Java求两个数组的交集、差集、并集
目录 Java求两个数组的并集,代码如下: Java求两个数组的交集,代码如下: Java求两个数组的差集,代码如下: 交集.差集.并集测试代码如下: Java求两个数组的并集,代码如下: /*** ...
- Java实验——定义一个类,该类中包含以下几个方法(静态):实现两个字符串数组的逆序排序,输出结果为字符串数组;求两个整形数组的交集;求两个浮点型数组的并集;
目录 一.实现两个字符串数组的逆序排序,输出结果为字符串数组 解题思路: 具体代码: 运行截图: 二.求两个整形数组的交集 具体代码: 运行截图: 三.求两个浮点型数组的并集 解题思路: 具体代码: ...
- java集合操作-----求两个集合的交集和并集
java求两个集合的交集和并集 java如何求两个集合的交集和并集呢??其实java的API中已经封装了方法.今天写个简单的例子测试一下:(例子中以java.util.LinkedList为例) 求连 ...
最新文章
- QIIME 2教程. 27语义类型Semantic(2021.2)
- ubuntu 10.0.4安装小企鹅(Fcitx)输入法
- 红帽系列linux自行配置本地yum源
- wordcount代码_通过腾讯云 Serverless Regsitry 快速开发与部署一个 WordCount 实例
- python合并txt文本_Python实现将目录中TXT合并成一个大TXT文件的方法
- ArrayList 面试10连问
- MIT“食人花”机器人,能抓起120倍重的物体,软硬皆可,不问形状
- Wireshark教程(简介、抓包、过滤器)
- 树——一种数据结构(二)
- python 选择多个文件_python-PyQt QFileDialog-多目录选择
- bzoj 4516: [Sdoi2016]生成魔咒
- 2021安徽安全员B证考试多选练习题库
- 松下PLC 三个单位的延时定时器指令的使用
- 系列学习 Lambda 表达式之第 2 篇 —— JDK1.8 的 Stream 流基本使用
- 免疫系统与冠状病毒之争:抗体水平下降时,T细胞会支持你
- 视频库:人工智能开发_人工智能工程师_AI人工智能
- gt 630 linux驱动下载,Ubuntu 13.04 双显卡安装NVIDIA GT 630M驱动
- Ubuntu X86编译安装Gstreamer nvenc插件(硬件编码)
- 常用汉语声母韵母及汉语拼音表
- 基于BlueZ的C语言蓝牙编程