如何基于特定值进行聚合并在给定对象列表的情况下创建嵌套地图? [英] How to aggregate based on certain value and create nested map given a list of objects?

查看:61
本文介绍了如何基于特定值进行聚合并在给定对象列表的情况下创建嵌套地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List<SingleDay>,其中SingleDay是:

class SingleDay{ 
      private Date date;
      private String County;

   // otherstuff
}

我想通过以下转换将其转换为Map<LocalDate, Map<String,SingleDay>>:

I would like to convert this into a Map<LocalDate, Map<String,SingleDay>> by the following transformation:

Key: 2020-05-27, Value: {'County1'=SingleDay [date=2020-05-27, County='County1'],
                         'County2'=SingleDay [date=2020-05-27, County='County2'], 
                         'County3'=SingleDay [date=2020-05-27, County='County3']
                        }
Key: 2020-05-28, Value: {'County4'=SingleDay [date=2020-05-28, County='County4'],
                         'County5'=SingleDay [date=2020-05-28, County='County5'], 
                         'County3'=SingleDay [date=2020-05-28, County='County3']
                        }

请注意,这是我之前提出的问题:

Note, this is a question that arose from my previous question : How do you use java stream api to convert list of objects into a nested map based on information stored inside object?

推荐答案

执行以下操作:

Map<LocalDate, Map<String, SingleDay>> result = list.stream()
            .collect(Collectors.groupingBy(SingleDay::getDate, Collectors.toMap(SingleDay::getCounty, e -> e)));

演示:

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

class SingleDay {
    private LocalDate date;
    private String County;

    public SingleDay(LocalDate date, String county) {
        this.date = date;
        County = county;
    }
    public LocalDate getDate() {
        return date;
    }

    public String getCounty() {
        return County;
    }

    @Override
    public String toString() {
        return "SingleDay [date=" + date + ", County=" + County + "]";
    }
    // otherstuff
}

public class Main {
    public static void main(String[] args) {
        List<SingleDay> list = List.of(
                new SingleDay(LocalDate.now(), "X"), 
                new SingleDay(LocalDate.now(), "Y"),
                new SingleDay(LocalDate.now(), "Z"), 
                new SingleDay(LocalDate.now().plusDays(1), "A"),
                new SingleDay(LocalDate.now().plusDays(1), "B"), 
                new SingleDay(LocalDate.now().plusDays(1), "C"));

        Map<LocalDate, Map<String, SingleDay>> result = list.stream()
                .collect(Collectors.groupingBy(SingleDay::getDate, Collectors.toMap(SingleDay::getCounty, e -> e)));

        // Display
        result.forEach((k, v) -> System.out.println("Key: " + k + ", Value: " + v));
    }
}

输出:

Key: 2020-05-27, Value: {A=SingleDay [date=2020-05-27, County=A], B=SingleDay [date=2020-05-27, County=B], C=SingleDay [date=2020-05-27, County=C]}
Key: 2020-05-26, Value: {X=SingleDay [date=2020-05-26, County=X], Y=SingleDay [date=2020-05-26, County=Y], Z=SingleDay [date=2020-05-26, County=Z]}

这篇关于如何基于特定值进行聚合并在给定对象列表的情况下创建嵌套地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆