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(Comparator.comparing(PlnexecutionRecord :: getTime)).collect(Collectors.toList()); }