Java8 stream根据字段分组并排序

Java8 stream根据字段分组并排序

1.根据字符串类型日期分组,并按照日期升序排序,返回TreeMap<String,List>,map的key为字符串日期,value为list

ArrayList<PlnexecutionRecord> records = plnExectionRecordMapper.selectRecord(ids[i]);
TreeMap<String,List<PlnexecutionRecord>> collect =  records.stream().collect(Collectors.groupingBy(PlnexecutionRecord :: getDtate,TreeMap::new,Collectors.toList()));

2.将value中的list按照时间早晚排序

Set<Entry<String,List<PlnexecutionRecord>>> entrySet = collect.entrySet();
for(Entry<String,List<PlnexecutionRecord>> entry : entrySet){
    List<PlnexecutionRecord> sortedList =  entry.getValue().stream().sorted(Comparing.comparing(PlnexecutionRecord :: getTime)).collect(Collectors.toList());
}

猜你喜欢

转载自blog.csdn.net/qq_32332777/article/details/103362411