实际需求,针对list根据数据时间分组,分组后的map无须,但需要按照时间正序排列,代码如下
// 先根据时间分组
Map<String, List<QcDataVO>> monthList = entry.getValue().stream().collect(Collectors.groupingBy(QcDataVO::getInspectCycle));
// 分组后根据key正序排列,()LinkedHashMap有序)
monthList = monthList.entrySet().stream().sorted(new Comparator<Map.Entry<String, List<QcDataVO>>>() {
@Override
public int compare(Map.Entry<String, List<QcDataVO>> o1, Map.Entry<String, List<QcDataVO>> o2) {
try {
Date d1 = DateUtil.convertStringToDate(o1.getKey(), "yyyy-MM");
Date d2 = DateUtil.convertStringToDate(o2.getKey(), "yyyy-MM");
return d1.compareTo(d2);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}).collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldVal, newVal) -> oldVal,
LinkedHashMap::new
));