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("英雄聯(lián)盟");
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);
}
}結(jié)果如下:
交集:[王者榮耀, 地下城與勇士]
差集:[英雄聯(lián)盟, 穿越火線]
并集:[王者榮耀, 英雄聯(lián)盟, 魔獸世界, 地下城與勇士, 穿越火線]
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的數(shù)據(jù)——差集 (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());
結(jié)果:
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的數(shù)據(jù)——交集
List<Student> intersection = list1.stream() ? ? ? ? ? ? ? ? ?? ??? ??? ?.filter(item -> list2.stream() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .map(e -> e.getId()) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .collect(Collectors.toList()) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .contains(item.getId())) ? ? ? ? ? ? ? ? ? ? ? ? ? ? .collect(Collectors.toList());
結(jié)果:
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);
結(jié)果:
結(jié)果:
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());
? ? ? ? }
? ? }
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
分布式開發(fā)醫(yī)療掛號系統(tǒng)數(shù)據(jù)字典模塊前后端實(shí)現(xiàn)
這篇文章主要為大家介紹了分布式開發(fā)醫(yī)療掛號系統(tǒng)數(shù)據(jù)字典模塊前后端實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
SpringMVC?bean加載控制的實(shí)現(xiàn)分析
SpringMVC是一種基于Java,實(shí)現(xiàn)了Web?MVC設(shè)計模式,請求驅(qū)動類型的輕量級Web框架,即使用了MVC架構(gòu)模式的思想,將Web層進(jìn)行職責(zé)解耦。基于請求驅(qū)動指的就是使用請求-響應(yīng)模型,框架的目的就是幫助我們簡化開發(fā),SpringMVC也是要簡化我們?nèi)粘eb開發(fā)2023-02-02
SpringBoot+Shiro+LayUI權(quán)限管理系統(tǒng)項目源碼
本項目旨在打造一個基于RBAC架構(gòu)模式的通用的、并不復(fù)雜但易用的權(quán)限管理系統(tǒng),通過SpringBoot+Shiro+LayUI權(quán)限管理系統(tǒng)項目可以更好的幫助我們掌握springboot知識點(diǎn),感興趣的朋友一起看看吧2021-04-04
MyBatis環(huán)境資源配置實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了MyBatis環(huán)境資源配置實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
RocketMQ4.5.2 修改mqnamesrv 和 mqbroker的日志路徑操作
這篇文章主要介紹了RocketMQ 4.5.2 修改mqnamesrv 和 mqbroker的日志路徑操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot+MyBatis簡單數(shù)據(jù)訪問應(yīng)用的實(shí)例代碼
這篇文章主要介紹了SpringBoot+MyBatis簡單數(shù)據(jù)訪問應(yīng)用的實(shí)例代碼,需要的朋友可以參考下2017-05-05

