Java之Set 交集,差集,并集的用法
作者:夏威夷8080
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()); } } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。