为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

哔哔one 2022-06-04 09:41:46
我有以下情况,两个邻里对象和同一条街道但不同的房子。我想知道使用流如何将我分组为一个邻域对象总之,我有以下数据Neighborhood: n11streets: s11houses: h1Neighborhood: n11streets: s11houses: h2我想合并它以显示如下Neighborhood: n11streets: s11houses: 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));
查看完整描述

2 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

好吧,您可以构建一个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<>();


    neighborhoods.forEach(neighborhood -> {


        Map<String, List<String>> m = map.computeIfAbsent(neighborhood.getId(), (key) -> new HashMap<>());

        neighborhood.getStreets()

                    .forEach(street -> {

                        m.merge(street.getId(),

                                street.getHouses()

                                      .stream()

                                      .map(House::getId)

                                      .collect(Collectors.toCollection(ArrayList::new)),

                                (oldV, newV) -> {

                                    oldV.addAll(newV);

                                    return oldV;

                                }

                        );

                    });

    });


查看完整回答
反对 回复 2022-06-04
?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

我不得不稍微更改您提供的代码:


import java.util.List;


public class Neighborhood {

    public String id;

    public List<Street> streets;


    public Neighborhood(String id, List<Street> streets) {

        this.id = id;

        this.streets = streets;

    }

}


class Street {


    public Street(String id, List<House> houses) {

        this.id = id;

        this.houses = houses;

    }


    public String id;

    public List<House> houses;

}


class House {

    public String id;


    public House(String id) {

        this.id = id;

    }

}

这是我的答案:


import java.util.*;

import java.util.stream.Collectors;

import java.util.stream.Stream;


public class Main {


    public static void main(String[] args) {

        House h1 = new House("h11");

        Street s1 = new Street("s11", Arrays.asList(h1));



        House h2 = new House("h12");

        Street s2 = new Street("s11", Arrays.asList(h2));


        Neighborhood n1 = new Neighborhood("n11", Arrays.asList(s1));

        Neighborhood n2 = new Neighborhood("n11", Arrays.asList(s2));


        Set<Street> collect = Stream.of(n1, n2).flatMap(neighborhood -> neighborhood.streets.stream())

                                          .collect(Collectors.toSet());


        System.out.println(collect);


        final Map<String, List<Street>> collect1 = collect.stream().collect(Collectors.groupingBy(street -> street.id));


        final List<Neighborhood> neighborhoods = new ArrayList<>();


        collect1.forEach((s, streets) -> {

            final List<House> collect2 = streets.stream().flatMap(street -> street.houses.stream())

                                                .collect(Collectors.toList());

            final Street street = new Street(s, collect2);

            neighborhoods.add(new Neighborhood(s, Arrays.asList(street)));

        });

    }

}


查看完整回答
反对 回复 2022-06-04
  • 2 回答
  • 0 关注
  • 192 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信