I have the following problem: Given these classes,
class Person {
private String zip;
...
public String getZip(){
return zip;
}
}
class
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:
//A Set 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 persons = ...;
List regions = ...;
Map> 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
you get a Stream
. Then you map each instance to a tuple
that contains all the regions. 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.
For example, given the input:
List persons = Arrays.asList(new Person("A"), new Person("B"), new Person("C"));
List regions =
Arrays.asList(new Region(Arrays.asList("A", "B")), new Region(Arrays.asList("A")));
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
.