How to aggregate based on certain value and create nested map given a list of objects?

自古美人都是妖i 提交于 2020-05-30 07:31:48

问题


I have a List<SingleDay> where SingleDay is:

class SingleDay{ 
      private Date date;
      private String County;

   // otherstuff
}

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?


回答1:


Do it as follows:

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

Demo:

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

Output:

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


来源:https://stackoverflow.com/questions/62027187/how-to-aggregate-based-on-certain-value-and-create-nested-map-given-a-list-of-ob

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!