在Java8中使用Lambda表达式对映射进行排序

在Java8中使用Lambda表达式对映射进行排序,lambda,java-8,functional-programming,java-stream,Lambda,Java 8,Functional Programming,Java Stream,我已经创建了一个带有比较器的映射,以按键排序,但是在填充映射之后,在填充数据之后没有应用任何顺序 SimpleDateFormat byDay = new SimpleDateFormat("ddMMyyyy"); Map<String, DoubleSummaryStatistics> menuStatisticsXDay = new TreeMap<String, DoubleSummaryStatistics>(

我已经创建了一个带有比较器的映射,以按键排序,但是在填充映射之后,在填充数据之后没有应用任何顺序

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())));
SimpleDataFormat byDay=新的SimpleDataFormat(“ddMMyyyy”);
地图菜单UstatisticsxDay=新树状图(
新比较器(){
@凌驾
公共int比较(字符串dateStr1、字符串dateStr12){
Date date1=新日期();
Date date2=新日期();
试一试{
date1=byDay.parse(dateStr1);
}捕获(解析异常){
}
试一试{
date2=byDay.parse(dateStr1);
}捕获(解析异常){
}
返回日期1.与(日期2)相比;
}
});
月经统计日=
menuPrices.stream().sorted(比较(MenuPrice::getUpdateDate))
.collect(Collectors.groupingBy(cp->byDay.format(cp.getUpdateDate()),
Collectors.summaringdouble(cp->cp.getpriceduble());
这样做会对键进行排序,但作为字符串,因此“06092018”将比“07082018”先,这就是为什么我想使用我的比较器,转换为日期并对其排序,然后“07082018”将比“06092018”先:

Map菜单自定义统计sxday=
menuPrices.stream().sorted(比较(MenuPrice::getUpdateDate))
.collect(Collectors.groupingBy(m->byDay.format(m.getUpdateDate()),
Collectors.summaringdouble(m->m.getpriceduble())
.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(oldValue,newValue)->oldValue,LinkedHashMap::new));
您可以尝试一下

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

LinkedHashMap
保持顺序。

然后使用LocalDate而不是字符串作为键:

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));
Map菜单自定义统计sxday=
menuPrices.stream().sorted(比较(MenuPrice::getUpdateDate))
.collect(收集器.groupingBy(m->m.getUpdateLocalDate(),
Collectors.summaringdouble(m->m.getpriceduble())
.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(oldValue,newValue)->oldValue,LinkedHashMap::new));

您的
TreeMap
是不相关的,因为您使用从流操作返回的一个完全不同的
Map
引用覆盖
menuStatisticsXDay
变量。除此之外,您处理潜在
ParseException
s的方式将导致不一致。
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));