Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort Map<YearMonth, List<LocalDate>> with java 8 lambda and streams

I have a list of sorted dates like this:

2016-07-07
2016-07-08
2016-07-09
2016-07-10
2016-07-11
2016-07-12
2016-07-13
...
2016-07-31
2016-08-01
2016-08-02
2016-08-03
...
2017-01-01
2017-01-02
2017-01-03
...

From this list I generate a Map<YearMonth, List<LocalDate>> with stream:

Map<YearMonth, List<LocalDate>> d = dates.stream().collect(Collectors.toList())
      .stream().collect(Collectors.groupingBy(date -> YearMonth.from(date)));

The output of that map looks like this:

{2016-12=[2016-12-01, 2016-12-02,...2016-12-31], 2016-11=[2016-11-01, 2016-11-02,...]}

But my needed output should look be like this:

  • Key of map sorted by date ascending: {2016-07=[...], 2016-08=[...]}
  • Value of map (List) sorted by date ascending: {2016-07=[2016-07-01, 2016-07-02, ...], 2016-08=[2016-08-01, 2016-08-02, ...]}

I tried many options to get my expected result but I just get the correct sorting for the key or the value and not for both:

Map<YearMonth, List<LocalDate>> m = stream().collect(Collectors.toList())
      .stream().sorted((e1,e2) -> e2.compareTo(e1))
      .collect(Collectors.groupingBy(date -> YearMonth.from(date)));

Result:

{2016-07=[2016-07-31, 2016-07-30, ...], 2016-08=[2016-08-31, 2016-08-30, ...]}

How can I sort both by key and value?

like image 622
Patrick Avatar asked Mar 08 '26 21:03

Patrick


2 Answers

Use a TreeMap as collector so the output is sorted by key.

Something like this:

 dates.stream()
      .sorted()
      .collect(
         Collectors.groupingBy(YearMonth::from, TreeMap::new, Collectors.toList())
      );
like image 73
k5_ Avatar answered Mar 11 '26 11:03

k5_


Collectors.groupingBy(date -> YearMonth.from(date)) internally stores result in HashMap and key sorting is lost.

This implementation will preserve keys order:

  Map<YearMonth, List<LocalDate>> d = dates
        .stream()
        .sorted((e1,e2) -> e2.compareTo(e1))
        .collect(Collectors
                .groupingBy(YearMonth::from,
                        LinkedHashMap::new,
                        Collectors.toList()));
like image 41
Andrew Kolpakov Avatar answered Mar 11 '26 11:03

Andrew Kolpakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!