[英]Merge two collections in Java
我有两张地图:
Map<String, Student> students1 = new HashMap<>();
students1.put("New York", new Student("John"));
students1.put("Canada", new Student("Robert"));
Map<String, Student> students2 = new HashMap<>();
students2.put("Chicago", new Student("Nick"));
students2.put("New York", new Student("Ann"));
结果,我想得到这个:
{Canada=Robert, New York=[John, Ann], Chicago=Nick}
我可以轻松地这样做:
Map<City, List<Student>> allStudents = new HashMap<>();
students1.forEach((currentCity, currentStudent) -> {
allStudents.computeIfPresent(currentCity, (city, studentsInCity) -> {
studentsInCity.add(currentStudent);
return studentsInCity;
});
allStudents.putIfAbsent(currentCity, new ArrayList<Student>() {
{
add(currentStudent);
}
});
});
// then again for the second list
但是有没有其他方法可以合并许多集合(在这种情况下是两个)? 是否有类似简短的 lambda 表达式,或来自某些集成 Java 库的方法等...?
您可以在任意数量的地图上创建一个流,然后在它们的条目上创建平面地图。 然后就像按条目的键分组一样简单,将条目的值映射到List
作为值:
Map<String, List<Student>> collect = Stream.of(students1, students2)
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
使用静态导入以提高可读性:
Map<String, List<Student>> collect = Stream.of(students1, students2)
.flatMap(map -> map.entrySet().stream())
.collect(groupingBy(Entry::getKey, mapping(Entry::getValue, toList())));
替换toList()
与toSet()
如果一个Set
是作为地图的值比较合适。
我认为 Magilex 提供的Stream
版本是最优雅的方式。 但我还是想再给一个选择。
static final Function<...> NEW_LIST = __ -> new ArrayList<>();
Map<City, List<Student>> allStudents = new HashMap<>();
students1.forEach((city, student) -> {
allStudents.computeIfAbsent(city, NEW_LIST).add(student);
});
https://www.baeldung.com/java-merge-maps这是一个链接,应该会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.