gpt4 book ai didi

java - 如何使用 Java 8 收集器对三重嵌套映射求和

转载 作者:行者123 更新时间:2023-12-04 09:32:07 25 4
gpt4 key购买 nike

我有这张 map
Map >>
EHourQuarter 是一个枚举:

public enum EHourQuarter {
FIRST(0, 14, 15),
SECOND(15, 29, 30),
THIRD(30, 44, 45),
FOURTH(45, 59, 60);

private Integer start;
private Integer end;
private Integer value;//this is for UI purposes
}
具有以下值:
{2020-07-07 -> {0 -> {EHourQuarter.FIRST -> 5.5, EHourQuarter.SECOND -> 10.2, ...},
1 -> {EHourQuarter.FIRST -> 33.2, EHourQuarter.SECOND -> 30.1, ...}, ...},
2020-07-08 -> {0 -> {EHourQuarter.FIRST -> 5.5, EHourQuarter.SECOND -> 10.2, ...},
1 -> {EHourQuarter.FIRST -> 33.2, EHourQuarter.SECOND -> 30.1, ...}, ... }
它是Double的EHourQuarter map 的Integer(小时:从0到23)的 map 的LocalDate的 map 。
我需要得到一个 Map > 包含每个日期的累积,这意味着如果日期 2020-07-07 到 2020-07-10(4 天)包含在小时 0每个 EHourQuarter 每一个 5,那么结果应该显示在 0 小时,每个季度的值为 20。
此外,如果通过这样做,您还可以帮助我将其映射到像这样的 DTO 列表,
public class QuarterlyOccupancyDTO {
private Integer hour;
private Integer minute;//this is the value property of EHourQuarter
private Double occupancy;
}
我会很感激的。
最后,DTO 列表应包含按小时和分钟分组的所有日期的总和(EHourQuarter 的 value 属性)。
这是一个例子。
注意: map 可以包含多个日期,目的是对所有 .
鉴于这张 map :
{
"2020-06-26":{
"0":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"1":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"2":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"3":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"4":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"5":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"6":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"7":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"8":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"9":{
"FOURTH":5.0,
"FIRST":5.0,
"THIRD":5.0,
"SECOND":5.0
},
"10":{
"FOURTH":5.0,
"FIRST":5.0,
"THIRD":5.0,
"SECOND":5.0
},
"11":{
"FOURTH":5.0,
"FIRST":5.0,
"THIRD":5.0,
"SECOND":5.0
},
"12":{
"FOURTH":5.0,
"FIRST":5.0,
"THIRD":5.0,
"SECOND":5.0
},
"13":{
"FOURTH":5.0,
"FIRST":5.0,
"THIRD":5.0,
"SECOND":5.0
},
"14":{
"FOURTH":5.0,
"FIRST":5.0,
"THIRD":5.0,
"SECOND":5.0
},
"15":{
"FOURTH":5.0,
"FIRST":5.0,
"THIRD":5.0,
"SECOND":5.0
},
"16":{
"FOURTH":5.0,
"FIRST":5.0,
"THIRD":5.0,
"SECOND":5.0
},
"17":{
"FOURTH":5.0,
"FIRST":5.0,
"THIRD":5.0,
"SECOND":5.0
},
"18":{
"FOURTH":0.0,
"FIRST":5.0,
"THIRD":0.0,
"SECOND":0.0
},
"19":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"20":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"21":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"22":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
},
"23":{
"FOURTH":0.0,
"FIRST":0.0,
"THIRD":0.0,
"SECOND":0.0
}
}
}
像这样的列表应该是答案:
[
{
"hour":0,
"minute":15,
"occupancy":0.0
},
{
"hour":0,
"minute":30,
"occupancy":0.0
},
{
"hour":0,
"minute":45,
"occupancy":0.0
},
{
"hour":0,
"minute":60,
"occupancy":0.0
},
{
"hour":1,
"minute":15,
"occupancy":0.0
},
{
"hour":1,
"minute":30,
"occupancy":0.0
},
{
"hour":1,
"minute":45,
"occupancy":0.0
},
{
"hour":1,
"minute":60,
"occupancy":0.0
},
{
"hour":2,
"minute":15,
"occupancy":0.0
},
{
"hour":2,
"minute":30,
"occupancy":0.0
},
{
"hour":2,
"minute":45,
"occupancy":0.0
},
{
"hour":2,
"minute":60,
"occupancy":0.0
},
{
"hour":3,
"minute":15,
"occupancy":0.0
},
{
"hour":3,
"minute":30,
"occupancy":0.0
},
{
"hour":3,
"minute":45,
"occupancy":0.0
},
{
"hour":3,
"minute":60,
"occupancy":0.0
},
{
"hour":4,
"minute":15,
"occupancy":0.0
},
{
"hour":4,
"minute":30,
"occupancy":0.0
},
{
"hour":4,
"minute":45,
"occupancy":0.0
},
{
"hour":4,
"minute":60,
"occupancy":0.0
},
{
"hour":5,
"minute":15,
"occupancy":0.0
},
{
"hour":5,
"minute":30,
"occupancy":0.0
},
{
"hour":5,
"minute":45,
"occupancy":0.0
},
{
"hour":5,
"minute":60,
"occupancy":0.0
},
{
"hour":6,
"minute":15,
"occupancy":0.0
},
{
"hour":6,
"minute":30,
"occupancy":0.0
},
{
"hour":6,
"minute":45,
"occupancy":0.0
},
{
"hour":6,
"minute":60,
"occupancy":0.0
},
{
"hour":7,
"minute":15,
"occupancy":0.0
},
{
"hour":7,
"minute":30,
"occupancy":0.0
},
{
"hour":7,
"minute":45,
"occupancy":0.0
},
{
"hour":7,
"minute":60,
"occupancy":0.0
},
{
"hour":8,
"minute":15,
"occupancy":0.0
},
{
"hour":8,
"minute":30,
"occupancy":0.0
},
{
"hour":8,
"minute":45,
"occupancy":0.0
},
{
"hour":8,
"minute":60,
"occupancy":0.0
},
{
"hour":9,
"minute":15,
"occupancy":5.0
},
{
"hour":9,
"minute":30,
"occupancy":5.0
},
{
"hour":9,
"minute":45,
"occupancy":5.0
},
{
"hour":9,
"minute":60,
"occupancy":5.0
},
{
"hour":10,
"minute":15,
"occupancy":5.0
},
{
"hour":10,
"minute":30,
"occupancy":5.0
},
{
"hour":10,
"minute":45,
"occupancy":5.0
},
{
"hour":10,
"minute":60,
"occupancy":5.0
},
{
"hour":11,
"minute":15,
"occupancy":5.0
},
{
"hour":11,
"minute":30,
"occupancy":5.0
},
{
"hour":11,
"minute":45,
"occupancy":5.0
},
{
"hour":11,
"minute":60,
"occupancy":5.0
},
{
"hour":12,
"minute":15,
"occupancy":5.0
},
{
"hour":12,
"minute":30,
"occupancy":5.0
},
{
"hour":12,
"minute":45,
"occupancy":5.0
},
{
"hour":12,
"minute":60,
"occupancy":5.0
},
{
"hour":13,
"minute":15,
"occupancy":5.0
},
{
"hour":13,
"minute":30,
"occupancy":5.0
},
{
"hour":13,
"minute":45,
"occupancy":5.0
},
{
"hour":13,
"minute":60,
"occupancy":5.0
},
{
"hour":14,
"minute":15,
"occupancy":5.0
},
{
"hour":14,
"minute":30,
"occupancy":5.0
},
{
"hour":14,
"minute":45,
"occupancy":5.0
},
{
"hour":14,
"minute":60,
"occupancy":5.0
},
{
"hour":15,
"minute":15,
"occupancy":5.0
},
{
"hour":15,
"minute":30,
"occupancy":5.0
},
{
"hour":15,
"minute":45,
"occupancy":5.0
},
{
"hour":15,
"minute":60,
"occupancy":5.0
},
{
"hour":16,
"minute":15,
"occupancy":5.0
},
{
"hour":16,
"minute":30,
"occupancy":5.0
},
{
"hour":16,
"minute":45,
"occupancy":5.0
},
{
"hour":16,
"minute":60,
"occupancy":5.0
},
{
"hour":17,
"minute":15,
"occupancy":5.0
},
{
"hour":17,
"minute":30,
"occupancy":5.0
},
{
"hour":17,
"minute":45,
"occupancy":5.0
},
{
"hour":17,
"minute":60,
"occupancy":5.0
},
{
"hour":18,
"minute":15,
"occupancy":5.0
},
{
"hour":18,
"minute":30,
"occupancy":0.0
},
{
"hour":18,
"minute":45,
"occupancy":0.0
},
{
"hour":18,
"minute":60,
"occupancy":0.0
},
{
"hour":19,
"minute":15,
"occupancy":0.0
},
{
"hour":19,
"minute":30,
"occupancy":0.0
},
{
"hour":19,
"minute":45,
"occupancy":0.0
},
{
"hour":19,
"minute":60,
"occupancy":0.0
},
{
"hour":20,
"minute":15,
"occupancy":0.0
},
{
"hour":20,
"minute":30,
"occupancy":0.0
},
{
"hour":20,
"minute":45,
"occupancy":0.0
},
{
"hour":20,
"minute":60,
"occupancy":0.0
},
{
"hour":21,
"minute":15,
"occupancy":0.0
},
{
"hour":21,
"minute":30,
"occupancy":0.0
},
{
"hour":21,
"minute":45,
"occupancy":0.0
},
{
"hour":21,
"minute":60,
"occupancy":0.0
},
{
"hour":22,
"minute":15,
"occupancy":0.0
},
{
"hour":22,
"minute":30,
"occupancy":0.0
},
{
"hour":22,
"minute":45,
"occupancy":0.0
},
{
"hour":22,
"minute":60,
"occupancy":0.0
},
{
"hour":23,
"minute":15,
"occupancy":0.0
},
{
"hour":23,
"minute":30,
"occupancy":0.0
},
{
"hour":23,
"minute":45,
"occupancy":0.0
},
{
"hour":23,
"minute":60,
"occupancy":0.0
}
]

最佳答案

第一次使用 flatMap创建 Stream<SimpleEntry<Integer, EHourQuarter>, Double>然后使用 toMap Collection 为 Map<SimpleEntry<Integer, EHourQuarter>, Double> .然后映射到你的 DTO 类。

List<QuarterlyOccupancyDTO> result = map.entrySet().stream()
.flatMap(d -> d.getValue().entrySet().stream()
.flatMap(h -> h.getValue().entrySet().stream().map(
e -> new SimpleEntry<>(new SimpleEntry<>(h.getKey(), e.getKey()), e.getValue()))))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a + b))
.entrySet()
.stream()
.map(m -> new QuarterlyOccupancyDTO(m.getKey().getKey(), m.getKey().getValue().getValue(), m.getValue()))
.collect(Collectors.toList());
注意:由于您没有显示代码,因此某些部分可能无法正常工作。全码 here

关于java - 如何使用 Java 8 收集器对三重嵌套映射求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62790358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com