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

时间:2018-05-31 04:58:42

标签: java java-8 java-stream

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

总的来说,它的cityArea里面有各种areaPin。每个areaPin有两个部分:1和2.我想按部分求和相应的计数,并希望每个cityId的聚合数据。请参考以下课程。所以就像我通过cityId + areaPin汇总信息一样,我希望cityId汇总信息。

CityArea:

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

}

ResourceCount:

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]}

2 个答案:

答案 0 :(得分:2)

试试这个,

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

答案 1 :(得分:0)

首先定义一个直接类来保存您的数据:

public class CityResource {
   String cityId;
   Integer areaPin;
   ResourceCount resource;
   // Constructor, getter, setter
}

然后汇总您的数据:

Map<CityArea, Map<Integer, ResourceCount>> input = ...;

Map<String, Map<Integer, ResourceCount>> output = 
input.entrySet().stream()
     .flatMap(e -> e.getValue().entrySet().stream().map(r -> new CityResource(e.getKey(), r.getKey(), r.getValue())))
     .collect(groupingBy(CityResource::getCityId, 
                  groupingBy(CityResource::getAreaPin, reducing(new ResourceCount(), (r1, r2) -> new ResourceCount(r1.getStreetLightCount() + r2.getStreetLightCount(), r1.getWaterTankCount() + r2.getWaterTankCount())))));
相关问题