List转为Map
resDocumentMap = documentLists.get(i).stream().collect(Collectors.toMap(e -> String.valueOf(e.get("_id")), e -> e));
List转为Set
Set<String> tmpDocumentSet = documentLists.get(i).stream().map(e -> String.valueOf(e.get("_id"))).collect(Collectors.toSet());
retainAll 保留公共的元素
public static void main(String[] args) {
Set<String> set1 = new HashSet<>();
Set<String> set2 = new HashSet<>();
set1.add("1");
set1.add("2");
set2.add("1");
set2.add("3");
set1.retainAll(set2);
// [1]
System.out.println(set1);
}
map.keySet()
public static void main(String[] args) {
Map<String, Student> map = new HashMap<>();
map.put("1", new Student());
map.put("2", new Student());
map.put("3", new Student());
Set<String> tmpSet = new HashSet<>();
tmpSet.add("1");
// Returns a Set view of the keys contained in this map. The set is backed by the map
// so changes to the map are reflected in the set, and vice-versa
// keySet()方法会返回map的set视图,更改map会影响set,反之亦然
Set<String> res = map.keySet();
res.retainAll(tmpSet);
// [1]
System.out.println(map.keySet());
}
guava MapDifference
不太符合预期,谨慎使用
import cn.hutool.json.JSONUtil;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Student {
private String id;
private String name;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) {
List<Student> students1 = new ArrayList<>();
List<Student> students2 = new ArrayList<>();
Student student11 = new Student();
Student student12 = new Student();
student11.setId("1");
student11.setName("zhangsan");
student11.setAge(18);
student12.setId("2");
student12.setName("lisi");
student12.setAge(20);
students1.add(student11);
students1.add(student12);
Student student21 = new Student();
Student student22 = new Student();
student21.setId("1");
student21.setName("zhangsan");
student21.setAge(18);
/*student22.setId("3");
student22.setName("wangwu");
student22.setAge(21);
students2.add(student21);
students2.add(student22);*/
student22.setId("2");
student22.setName("lisi");
student22.setAge(20);
students2.add(student21);
students2.add(student22);
Map<String ,Student> studentMap1 = students1.stream().collect(Collectors.toMap(e-> e.getId(), e -> e));
System.out.println("====> studentMap1");
// {"1":{"name":"zhangsan","id":"1","age":18},"2":{"name":"lisi","id":"2","age":20}}
System.out.println(JSONUtil.toJsonStr(studentMap1));;
Map<String ,Student> studentMap2 = students2.stream().collect(Collectors.toMap(e-> e.getId(), e -> e));
System.out.println("====> studentMap2");
System.out.println(JSONUtil.toJsonStr(studentMap2));
// // {"1":{"name":"zhangsan","id":"1","age":18},"3":{"name":"wangwu","id":"3","age":21}}
MapDifference mapDifference = Maps.difference(studentMap1, studentMap2);
System.out.println("====> studentMap1, studentMap2 are equal: ");
// false
System.out.println(mapDifference.areEqual());
Map<String, Student> entriesInCommon = mapDifference.entriesInCommon();
System.out.println("====> entriesInCommon 集合交集");
// 返回{}, 对象内容相同,但是是两个不同的对象也会认为是不同的对象
System.out.println(JSONUtil.toJsonStr(entriesInCommon));
Map<String, Student> entriesDiffering = mapDifference.entriesDiffering();
System.out.println("====> entriesDiffering 键相同,值不同");
// {"1":{}}
System.out.println(JSONUtil.toJsonStr(entriesDiffering));
Map<String, Student> entriesOnlyOnLeft = mapDifference.entriesOnlyOnLeft();
System.out.println("====> entriesOnlyOnLeft");
// {"2":{"name":"lisi","id":"2","age":20}}
System.out.println(JSONUtil.toJsonStr(entriesOnlyOnLeft));
Map<String, Student> entriesOnlyOnRight = mapDifference.entriesOnlyOnRight();
System.out.println("====> entriesOnlyOnRight");
// {"3":{"name":"wangwu","id":"3","age":21}}
System.out.println(JSONUtil.toJsonStr(entriesOnlyOnRight));
}
}
参考
【1】List转Map的三种方法:https://blog.csdn.net/linsongbin1/article/details/79801952
【2】Java Map 求交集 并集 差集
【3】Guava - Maps.difference:https://blog.csdn.net/Dream_Weave/article/details/109390841
网友评论