Java之Set 交集,差集,并集的用法
Java之Set 交集,差集,并集
/**
* Created by yuhui on 2017/7/11 0011.
*/
import java.util.HashSet;
import java.util.Set;
public class TestSet {
public static void main(String[] args) {
Set<String> result = new HashSet<String>();
Set<String> set1 = new HashSet<String>() {
{
add("王者荣耀");
add("英雄联盟");
add("穿越火线");
add("地下城与勇士");
}
};
Set<String> set2 = new HashSet<String>() {
{
add("王者荣耀");
add("地下城与勇士");
add("魔兽世界");
}
};
result.clear();
result.addAll(set1);
result.retainAll(set2);
System.out.println("交集:" + result);
result.clear();
result.addAll(set1);
result.removeAll(set2);
System.out.println("差集:" + result);
result.clear();
result.addAll(set1);
result.addAll(set2);
System.out.println("并集:" + result);
}
}结果如下:
交集:[王者荣耀, 地下城与勇士]
差集:[英雄联盟, 穿越火线]
并集:[王者荣耀, 英雄联盟, 魔兽世界, 地下城与勇士, 穿越火线]
java8 list<bean>交集差集并集
定义bean
public class Student {
private String Id ;
private String name;
private String age;
public void setAge(String age) {
this.age = age;
}
public String getAge() {
return age;
}
public String getId() {
return Id;
}
public String getName() {
return name;
}
public void setId(String id) {
Id = id;
}
public void setName(String name) {
this.name = name;
}
}定义两个list
list 1
List<Student> list1 = new ArrayList<Student>();
Student st1 = new Student();
st1.setId("2");
st1.setName("");
st1.setAge("");
list1.add(st1);
Student sta = new Student();
sta.setId("1");
sta.setName("");
sta.setAge("");
list1.add(sta);list 2
List<Student> list2 = new ArrayList<Student>();
Student st2 = new Student();
st2.setId("2");
st2.setName("");
st1.setAge("");
list2.add(st2);找出id存在list1不存在list2的数据——差集 (list1 - list2) // 差集 (list1 - list2) List<Student> distinctByUniqueList = list1.stream() .filter(item -> !list2.stream() .map(e -> e.getId() ) .collect(Collectors.toList()) .contains(item.getId())) .collect(Collectors.toList());
结果:
System.out.println("---差集 reduce1 (list1 - list2)---");
for(Student st : distinctByUniqueList){
System.out.println(st.getId());
System.out.println(st.getName());
}---差集 reduce1 (list1 - list2)---
1
找出id存在list1同时存在list2的数据——交集
List<Student> intersection = list1.stream() .filter(item -> list2.stream() .map(e -> e.getId()) .collect(Collectors.toList()) .contains(item.getId())) .collect(Collectors.toList());
结果:
System.out.println("---交集 intersection---");
for(Student st : intersection){
System.out.println(st.getId());
System.out.println(st.getName());
}---交集 intersection---
2
获取distinctByUniqueList 与intersection并集
List<Student> add1 = istinctByUniqueList.parallelStream().collect(toList()); List<Student> add2 = intersection.parallelStream().collect(toList()); add1.addAll(add2);
结果:
结果:
System.out.println("---并集 listAll---");
for(Student st : add1){
System.out.println(st.getId());
System.out.println(st.getName());
}
---并集 listAll---
21
源码
import java.util.ArrayList;
import java.util.List;
import java.util.stream.*;
import static java.util.stream.Collectors.toList;
public class test {
public static void main(String[] args) {
List<Student> list1 = new ArrayList<Student>();
Student st1 = new Student();
st1.setId("2");
st1.setName("");
st1.setAge("");
list1.add(st1);
Student sta = new Student();
sta.setId("1");
sta.setName("");
sta.setAge("");
list1.add(sta);
List<Student> list2 = new ArrayList<Student>();
Student st2 = new Student();
st2.setId("3");
st2.setName("");
st1.setAge("");
list2.add(st2);
// 差集 (list1 - list2)
List<Student> distinctByUniqueList = list1.stream()
.filter(item -> !list2.stream()
.map(e -> e.getId() )
.collect(Collectors.toList())
.contains(item.getId()))
.collect(Collectors.toList());
System.out.println("---差集 reduce1 (list1 - list2)---");
for(Student st : distinctByUniqueList){
System.out.println(st.getId());
System.out.println(st.getName());
}
// 交集
List<Student> intersection = list1.stream()
.filter(item -> list2.stream()
.map(e -> e.getId())
.collect(Collectors.toList())
.contains(item.getId()))
.collect(Collectors.toList());
System.out.println("---交集 intersection---");
for(Student st : intersection){
System.out.println(st.getId());
System.out.println(st.getName());
}
List<Student> add1 = distinctByUniqueList.parallelStream().collect(toList());
List<Student> add2 = intersection.parallelStream().collect(toList());
add1.addAll(add2);
System.out.println("---并集 listAll---");
for(Student st : add1){
System.out.println(st.getId());
System.out.println(st.getName());
}
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Java开发Spark应用程序自定义PipeLineStage详解
这篇文章主要为大家介绍了Java开发Spark应用程序自定义PipeLineStage详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-02-02
SpringBoot如何实现调用controller和Service层方法
文章介绍了在SpringBoot中如何在工具类中调用Controller和Service层的方法,通过创建一个工具类SpringUtil,并在Spring Boot启动类中进行配置扫描注入,工具类就可以访问Controller和Service层的方法2025-03-03
Spring整合Quartz Job以及Spring Task的实现方法
下面小编就为大家分享一篇Spring整合Quartz Job以及Spring Task的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2017-12-12
详解Spring框架下向异步线程传递HttpServletRequest参数的坑
这篇文章主要介绍了详解Spring框架下向异步线程传递HttpServletRequest参数的坑,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2019-03-03


最新评论