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;
? ? }
}定義兩個(gè)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同時(shí)存在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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java開(kāi)發(fā)Spark應(yīng)用程序自定義PipeLineStage詳解
這篇文章主要為大家介紹了Java開(kāi)發(fā)Spark應(yīng)用程序自定義PipeLineStage詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
SpringBoot如何實(shí)現(xiàn)調(diào)用controller和Service層方法
文章介紹了在SpringBoot中如何在工具類(lèi)中調(diào)用Controller和Service層的方法,通過(guò)創(chuàng)建一個(gè)工具類(lèi)SpringUtil,并在Spring?Boot啟動(dòng)類(lèi)中進(jìn)行配置掃描注入,工具類(lèi)就可以訪問(wèn)Controller和Service層的方法2025-03-03
Spring整合Quartz Job以及Spring Task的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Spring整合Quartz Job以及Spring Task的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Java Http多次請(qǐng)求復(fù)用同一連接示例詳解
Java?ArrayList集合之解鎖數(shù)據(jù)存儲(chǔ)新姿勢(shì)
詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑

