[英]How to group elements of a List by elements of another in Java 8
I have the following problem: Given these classes, 我有以下问题:鉴于这些类,
class Person {
private String zip;
...
public String getZip(){
return zip;
}
}
class Region {
private List<String> zipCodes;
...
public List<String> getZipCodes() {
return zipCodes;
}
}
using the Java 8 Stream API, how do I obtain a Map<Person, List<Region>>
based on whether the Region
contains that Person
's zip code? 使用Java 8 Stream API,如何根据Region
是否包含Person
的邮政编码获取Map<Person, List<Region>>
? In other words how do I group the regions by the people whose zip codes belong to those regions? 换句话说,如何通过邮政编码属于这些区域的人对区域进行分组?
I've done it in Java 7 the old fashioned way, but now I have to migrate the code to take advantage of the new features in Java 8. 我已经用Java 7老式地完成了它,但现在我必须迁移代码以利用Java 8中的新功能。
Thank you, 谢谢,
Impeto 英佩特
I suspect the cleanest way to do this -- I'm not quite happy with the other answers posted -- would be 我怀疑这是最干净的方式 - 我对发布的其他答案不太满意 - 会是
persons.stream().collect(Collectors.toMap(
person -> person,
person -> regions.stream()
.filter(region -> region.getZipCodes().contains(person.getZip()))
.collect(Collectors.toList())));
The original answer does an unnecessary mapping with tuples, so you see there the final solution. 原始答案使用元组进行不必要的映射,因此您可以看到最终的解决方案。 You could remove the mapping, and simply filter directly the regions
list: 您可以删除映射,只需直接过滤regions
列表:
//A Set<Region> is more appropriate, IMO
.stream()
.collect(toMap(p -> p,
p -> regions.stream()
.filter(r -> r.getZipCodes().contains(p.getZip()))
.collect(toSet())));
import java.util.AbstractMap.SimpleEntry; import static java.util.stream.Collectors.toMap; import static java.util.stream.Collectors.toList; ... List<Person> persons = ...; List<Region> regions = ...; Map<Person, List<Region>> map = persons.stream() .map(p -> new SimpleEntry<>(p, regions)) .collect(toMap(SimpleEntry::getKey, e -> e.getValue().stream() .filter(r -> r.getZipCodes().contains(e.getKey().getZip())) .collect(toList())));
From the List<Person>
you get a Stream<Person>
. 从List<Person>
您将获得Stream<Person>
。 Then you map each instance to a tuple <Person, List<Region>>
that contains all the regions. 然后将每个实例映射到包含所有区域的元组<Person, List<Region>>
。 From there, you collect the data in a map with the toMap
collector and, for each person, you build a List of Region
that contains the zip code of that person. 从那里,您使用toMap
收集器在地图中收集数据,并为每个人构建一个包含该人的邮政编码的Region
列表。
For example, given the input: 例如,给定输入:
Person{zip='A'} => [Region{zipCodes=[A, B]}, Region{zipCodes=[A]}]
Person{zip='B'} => [Region{zipCodes=[A, B]}]
Person{zip='C'} => []
It outputs: 它输出:
Person{zip='A'} => [Region{zipCodes=[A, B]}, Region{zipCodes=[A]}] Person{zip='B'} => [Region{zipCodes=[A, B]}] Person{zip='C'} => []
Also I guess the zipCodes
for each Region
could be a Set
. 另外我猜每个Region
的zipCodes
都可以是Set
。
I have not done any testing of this code, but it compiles so it must be right (:eyeroll:). 我还没有对这段代码进行任何测试,但它编译得一定是对的(:eyeroll :)。
public Map<Person,List<Region>> mapPeopleToRegion(List<Person> people, List<Region> regions){
final Map<Person,List<Region>> personToRegion = new HashMap<>();
people.forEach(person ->
personToRegion.put(
person,regions.stream().filter(
region -> region.getZipCodes().contains(person.getZip()))
.collect(Collectors.toList())));
return personToRegion;
}
It's still pretty ugly, and I think it would be improved by changing how you model things a bit, but I've only managed to come up with the following so far: 它仍然非常难看,而且我认为通过改变你对它的建模方式可以改进它,但到目前为止我只能设法得到以下内容:
public static void main(String[] args) {
Person[] people = {new Person("00001"), new Person("00002"), new Person("00005")};
Region[] regions = {
new Region("Region 1", Arrays.asList("00001", "00002", "00003")),
new Region("Region 2", Arrays.asList("00002", "00003", "00004")),
new Region("Region 3", Arrays.asList("00001", "00002", "00005"))
};
Map<Person, List<Region>> result = Stream.of(regions)
.flatMap(region -> region.getZipCodes().stream()
.map(zip -> new SimpleEntry<>(zip, region)))
.flatMap(entry -> Stream.of(people)
.filter(person -> person.getZip().equals(entry.getKey()))
.map(person -> new SimpleEntry<>(person, entry.getValue())))
.collect(Collectors.groupingBy(Entry::getKey, Collectors.mapping(Entry::getValue, Collectors.toList())));
result.entrySet().forEach(entry -> System.out.printf("[%s]: {%s}\n", entry.getKey(), entry.getValue()));
// Output:
// [Person: 0]: {[name: Region 1, name: Region 3]}
// [Person: 1]: {[name: Region 1, name: Region 2, name: Region 3]}
// [Person: 2]: {[name: Region 3]}
}
Having a ZipCode
class that contained the mapping and could be keyed on would make things cleaner: 拥有一个包含映射并且可以键入的ZipCode
类可以使事情更清晰:
public static void main(String[] args) {
Region r1 = new Region("Region 1");
Region r2 = new Region("Region 2");
Region r3 = new Region("Region 3");
ZipCode zipCode1 = new ZipCode("00001", Arrays.asList(r1, r3));
ZipCode zipCode2 = new ZipCode("00002", Arrays.asList(r1, r2, r3));
ZipCode zipCode3 = new ZipCode("00003", Arrays.asList());
ZipCode zipCode4 = new ZipCode("00004", Arrays.asList());
ZipCode zipCode5 = new ZipCode("00005", Arrays.asList(r3));
Person[] people = {
new Person(zipCode1),
new Person(zipCode2),
new Person(zipCode5)
};
Map<Person, List<Region>> result = Stream.of(people)
.collect(Collectors.toMap(person -> person,
person -> person.getZip().getRegions()));
result.entrySet().forEach(entry -> System.out.printf("[%s]: {%s}\n", entry.getKey(), entry.getValue()));
// Output:
// [Person: 0]: {[name: Region 1, name: Region 3]}
// [Person: 1]: {[name: Region 1, name: Region 2, name: Region 3]}
// [Person: 2]: {[name: Region 3]}
}
Some of the other answers contain code that does a lot of linear searching through lists. 其他一些答案包含对列表进行大量线性搜索的代码。 I think the Java 8 Stream solution should not be much slower than the classical variant. 我认为Java 8 Stream解决方案不应该比经典变体慢得多。 So here is a solution that takes advantage of Streams without sacrificing much performance. 所以这是一个利用Streams而不牺牲很多性能的解决方案。
List<Person> people = ...
List<Region> regions = ...
Map<String, List<Region>> zipToRegions =
regions.stream().collect(
() -> new HashMap<>(),
(map, region) -> {
for(String zipCode: region.getZipCodes()) {
List<Region> list = map.get(zipCode);
if(list == null) list = new ArrayList<>();
list.add(region);
map.put(zipCode, list);
}
},
(m1, m2) -> m1.putAll(m2)
);
Map<Person, List<Region>> personToRegions =
people.stream().collect(
Collectors.toMap(person -> person,
person -> zipToRegions.get(person.getZip()))
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.