java8通过合并对象或聚合进行流分组

我有以下情况,两个邻里对象和同一条街道但不同的房子。


我想知道使用流如何将我分组为一个邻域对象


总之,我有以下数据


Neighborhood: n11

streets: s11

houses: h1


Neighborhood: n11

streets: s11

houses: h2

我想合并它以显示如下


Neighborhood: n11

streets: s11

houses: h1,h2

代码如下


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));


哔哔one
浏览 193回答 2
2回答

慕标5832272

好吧,您可以构建一个Map<String, Map<String, List<String>>>,其中 key 是Neighborhood::id并且 value 是 a Map,其中 key 和 value 是Street::idList House::id。从这里将它重新构建回你想要的任何东西都留给你一个练习......Map<String, Map<String, List<String>>> map = new HashMap<>();&nbsp; &nbsp; neighborhoods.forEach(neighborhood -> {&nbsp; &nbsp; &nbsp; &nbsp; Map<String, List<String>> m = map.computeIfAbsent(neighborhood.getId(), (key) -> new HashMap<>());&nbsp; &nbsp; &nbsp; &nbsp; neighborhood.getStreets()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(street -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.merge(street.getId(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; street.getHouses()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(House::getId)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toCollection(ArrayList::new)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (oldV, newV) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oldV.addAll(newV);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return oldV;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });

蝴蝶刀刀

我不得不稍微更改您提供的代码:import java.util.List;public class Neighborhood {&nbsp; &nbsp; public String id;&nbsp; &nbsp; public List<Street> streets;&nbsp; &nbsp; public Neighborhood(String id, List<Street> streets) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; this.streets = streets;&nbsp; &nbsp; }}class Street {&nbsp; &nbsp; public Street(String id, List<House> houses) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; this.houses = houses;&nbsp; &nbsp; }&nbsp; &nbsp; public String id;&nbsp; &nbsp; public List<House> houses;}class House {&nbsp; &nbsp; public String id;&nbsp; &nbsp; public House(String id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }}这是我的答案:import java.util.*;import java.util.stream.Collectors;import java.util.stream.Stream;public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; House h1 = new House("h11");&nbsp; &nbsp; &nbsp; &nbsp; Street s1 = new Street("s11", Arrays.asList(h1));&nbsp; &nbsp; &nbsp; &nbsp; House h2 = new House("h12");&nbsp; &nbsp; &nbsp; &nbsp; Street s2 = new Street("s11", Arrays.asList(h2));&nbsp; &nbsp; &nbsp; &nbsp; Neighborhood n1 = new Neighborhood("n11", Arrays.asList(s1));&nbsp; &nbsp; &nbsp; &nbsp; Neighborhood n2 = new Neighborhood("n11", Arrays.asList(s2));&nbsp; &nbsp; &nbsp; &nbsp; Set<Street> collect = Stream.of(n1, n2).flatMap(neighborhood -> neighborhood.streets.stream())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toSet());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(collect);&nbsp; &nbsp; &nbsp; &nbsp; final Map<String, List<Street>> collect1 = collect.stream().collect(Collectors.groupingBy(street -> street.id));&nbsp; &nbsp; &nbsp; &nbsp; final List<Neighborhood> neighborhoods = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; collect1.forEach((s, streets) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final List<House> collect2 = streets.stream().flatMap(street -> street.houses.stream())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Street street = new Street(s, collect2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neighborhoods.add(new Neighborhood(s, Arrays.asList(street)));&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java