Nuñito Calzada
Nuñito Calzada

Reputation: 2076

Ordering a Map using Lambda Expressions in Java8

I have created a Map with a Comparator to order by the key, but after filling the Map there is no order applied after the data is populated.

SimpleDateFormat byDay = new SimpleDateFormat("ddMMyyyy");  

    Map<String, DoubleSummaryStatistics> menuStatisticsXDay = new TreeMap<String, DoubleSummaryStatistics>(

                        new Comparator<String>() {

                            @Override
                            public int compare(String dateStr1, String dateStr12) {
                                Date date1 = new Date();
                                Date date2 = new Date();
                                try {
                                    date1 = byDay.parse(dateStr1);
                                } catch (ParseException e) {
                                }
                                try {
                                    date2 = byDay.parse(dateStr1);
                                } catch (ParseException e) {
                                }

                                return date1.compareTo(date2);
                            }

                        });

                menuStatisticsXDay =
        menuPrices.stream().sorted(comparing(MenuPrice::getUpdateDate))
                                .collect(Collectors.groupingBy(cp -> byDay.format(cp.getUpdateDate()),
                                        Collectors.summarizingDouble(cp -> cp.getPriceInDouble())));

Doing this it sorts the keys, but as Strings, so "06092018" will be first than "07082018", thats why I want to user my comparator, convert to Date and order it, then "07082018" will be first than "06092018":

Map<String, DoubleSummaryStatistics> menuStatisticsXDay =
        menuPrices.stream().sorted(comparing(MenuPrice::getUpdateDate))
                        .collect(Collectors.groupingBy(m -> byDay.format(m.getUpdateDate()),
                                 Collectors.summarizingDouble(m -> m.getPriceInDouble())))
                        .entrySet().stream()
                        .sorted(Map.Entry.comparingByKey())
                        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(oldValue, newValue) -> oldValue, LinkedHashMap::new));

Upvotes: 1

Views: 141

Answers (2)

en Lopes
en Lopes

Reputation: 2123

Then work with LocalDate instead of String as a key:

Map<LocalDate, DoubleSummaryStatistics> menuStatisticsXDay =
                        menuPrices.stream().sorted(comparing(MenuPrice::getUpdateDate))
                                .collect(Collectors.groupingBy(m -> m.getUpdateLocalDate(),
                                         Collectors.summarizingDouble(m -> m.getPriceInDouble())))
                                .entrySet().stream()
                                .sorted(Map.Entry.comparingByKey())
                                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(oldValue, newValue) -> oldValue, LinkedHashMap::new));

Upvotes: 2

Ravindra Ranwala
Ravindra Ranwala

Reputation: 21124

You may try this out,

menuStatisticsXDay = menuPrices.stream().sorted(comparing(MenuPrice::getUpdateDate))
    .collect(Collectors.groupingBy(cp -> byDay.format(cp.getUpdateDate()), LinkedHashMap::new
        Collectors.summarizingDouble(cp -> cp.getPriceInDouble())));

The LinkedHashMap keeps the order.

Upvotes: 1

Related Questions