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