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());
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章