java - 如何使用Java8流和groupBy从 map 中获取多级聚合结果?

标签 java java-8 java-stream

我想通过使用流分组功能从我的 map 中获取聚合输出。 基本上我想要使用流的多级 groupby。我有一张包含一些数据的 map ,并希望将 map 作为包含聚合数据的输出。

总的来说,它有 cityArea 和里面的各种 areaPin。每个 areaPin 都有两个部分:1 和 2。我想按部分对相应的计数求和,并希望每个 cityId 的聚合数据。请引用以下类(class)。所以就好像我已经通过 cityId+areaPin 聚合了信息,我想从中提取 cityId 的聚合信息。

城市区域:

public class CityArea {
String cityId;
String areaPin;
public String getCityId() {
    return cityId;
}
public void setCityId(String cityId) {
    this.cityId = cityId;
}
public String getAreaPin() {
    return areaPin;
}
public void setAreaPin(String areaPin) {
    this.areaPin = areaPin;
}
public CityArea(String cityId, String areaPin) {
    super();
    this.cityId = cityId;
    this.areaPin = areaPin;
}
public CityArea() {
    super();
}

资源计数:

public class ResourceCount {

Integer streetLightCount;
Integer waterTankCount;
public Integer getStreetLightCount() {
    return streetLightCount;
}
public void setStreetLightCount(Integer streetLightCount) {
    this.streetLightCount = streetLightCount;
}
public Integer getWaterTankCount() {
    return waterTankCount;
}
public void setWaterTankCount(Integer waterTankCount) {
    this.waterTankCount = waterTankCount;
}
public ResourceCount(Integer streetLightCount, Integer waterTankCount) {
    super();
    this.streetLightCount = streetLightCount;
    this.waterTankCount = waterTankCount;
}
public ResourceCount() {
    super();
}

示例代码:

Map<CityArea, Map<Integer,ResourceCount>> map = new HashMap<>();

    CityArea ca1= new CityArea("cityId1", "1");
    CityArea ca2= new CityArea("cityId1", "2");

    CityArea ca3= new CityArea("cityId2", "1");
    CityArea ca4= new CityArea("cityId2", "2");

    ResourceCount resourceCount1 = new ResourceCount(10, 10);
    ResourceCount resourceCount2 = new ResourceCount(10, 10);

    Map<Integer,ResourceCount> resourceMap1 = new HashMap<>();
    resourceMap1.put(1, resourceCount1);
    resourceMap1.put(2, resourceCount2);

    map.put(ca1, resourceMap1);
    map.put(ca2, resourceMap1);

    Map<Integer,ResourceCount> resourceMap2 = new HashMap<>();
    resourceMap2.put(1, resourceCount1);
    resourceMap2.put(2, resourceCount2);

    map.put(ca3, resourceMap2);
    map.put(ca4, resourceMap2);

输入:

  {
  CityArea [cityId=cityId2, areaPin=2]=
  {1=ResourceCount [streetLightCount=10, waterTankCount=10],
   2=ResourceCount [streetLightCount=20, waterTankCount=20]},


 CityArea [cityId=cityId1, areaPin=2]=
 {1=ResourceCount [streetLightCount=10, waterTankCount=10],
  2=ResourceCount [streetLightCount=20, waterTankCount=20]}, 

 CityArea [cityId=cityId1, areaPin=1]=
 {1=ResourceCount [streetLightCount=10, waterTankCount=10],
 2=ResourceCount [streetLightCount=20, waterTankCount=20]},

 CityArea [cityId=cityId2, areaPin=1]=
 {1=ResourceCount [streetLightCount=10, waterTankCount=10],
 2=ResourceCount [streetLightCount=20, waterTankCount=20]}

 }

预期输出图:

Map<String, Map<Integer, ResourceCount>>

cityId1 = {1=ResourceCount [streetLightCount=20, waterTankCount=20],
            2=ResourceCount [streetLightCount=40, waterTankCount=40]},

cityId2 = {1=ResourceCount [streetLightCount=20, waterTankCount=20],
            2=ResourceCount [streetLightCount=40, waterTankCount=40]}

最佳答案

试试这个,

final Map<String, Map<Integer, ResourceCount>> destMap = map.entrySet().stream().collect(
        Collectors.toMap(entry -> entry.getKey().getCityId(), Map.Entry::getValue, (resMap1, resMap2) -> {
            return Stream.of(resMap1, resMap2).flatMap(m -> m.entrySet().stream())
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                            (rc1, rc2) -> new ResourceCount(
                                    rc1.getStreetLightCount() + rc2.getStreetLightCount(),
                                    rc1.getWaterTankCount() + rc2.getWaterTankCount())));
        }));

关于java - 如何使用Java8流和groupBy从 map 中获取多级聚合结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50616912/

相关文章:

java - 使用 .map() 编写 for 循环

java - Java 8 方法引用 - 只能用于消费者功能接口(interface)

java - 为什么java streams跳过方法强制执行其他操作

java - Jena 模型将我的 RDF 类型显式声明转换为隐式声明并弄乱了格式

java - 使用 jdbc 显示 sql 中的列值

Java 8 流 - 将列表项转换为子类类型

Java:许多键映射之一

Java 8 条件过滤和收集自定义 map

java - CXF:如何拦截 SOAP 响应

java - 如何迭代类中的多个枚举