I have following situation, where two neighborhood objects and same street but different houses.
i am wondering using stream how can i group into one neighborhood object
in summary i have following data
Neighborhood: n11
streets: s11
houses: h1
Neighborhood: n11
streets: s11
houses: h2
and i want to merge it to display like as following
Neighborhood: n11
streets: s11
houses: h1,h2
code as follows
public class Neighborhood{
public UUID id;
public List<Street> streets;
public Neighborhood(UUID id, List<Street> streets)
{
this.id=id;
this.streets=streets;
}
}
public class Streets
{
public UUID id;
public List<House> houses;
public Streets(UUID id, List<House> houses)
{
this.id=id;
this.houses=houses
}
}
public class House
{
public UUID id;
public House(id)
{
this.id=id;
}
}
House h1= new House("h11")
Street s1= new Street("s11", asList(h1))
Neighborhood n1 = new Neighborhood("n11", asList(s1));
House h2= new House("h12")
Street s2= new Street("s11", asList(h2))
Neighborhood n2 = new Neighborhood("n11", asList(s2));
Collectors.toMap( Neighborhood::getId, Function.identity(), (left, right) -> {merge two neighborhood}... but this merge is quite a beast. you would have to collect again to aMapso that you end up with aMap<String, List<String>>where Key isStreet::idand Value is a List of House ids's and from that build a Neighborhood back...