1.业务需求
当时期望在通过groupingBy后,进行字段有序排列。但是一直未找到单使用groupingBy无法实现分组后对,分组的list进行内部直接排序的方。
通过groupingBy后转为LinkedHashMap已实现有序排列,通过遍历,对目标字段进行排序。实现要求的不同排序需求。
public static void main(String[] args) {
List<PcSystem> arrayList = new ArrayList();
arrayList.add(new PcSystem("11","3"));
arrayList.add(new PcSystem("12","4"));
arrayList.add(new PcSystem("12","5"));
arrayList.add(new PcSystem("12","6"));
arrayList.add(new PcSystem("13","7"));
arrayList.add(new PcSystem("13","8"));
arrayList.add(new PcSystem("13","9"));
arrayList.add(new PcSystem("13","10"));
arrayList.add(new PcSystem("11","1"));
arrayList.add(new PcSystem("11","2"));
List<PcSystem> collect1 = arrayList.stream().sorted(Comparator.comparing(pcSystem -> pcSystem.getUserId())).collect(Collectors.toList());
List<PcSystem> collect2 = arrayList.stream().sorted(Comparator.comparing(PcSystem::getUserId).reversed()).collect(Collectors.toList());
LinkedHashMap<String, List<PcSystem>> groupMap1 = collect1.stream().collect(Collectors.groupingBy(PcSystem::getId,LinkedHashMap::new,Collectors.toList()));
LinkedHashMap<String, List<PcSystem>> groupMap2 = collect2.stream().collect(Collectors.groupingBy(PcSystem::getId,LinkedHashMap::new,Collectors.toList()));
System.out.println(groupMap1);
System.out.println(groupMap2);
for(Map.Entry<String, List<PcSystem>> entry : groupMap1.entrySet()){
List<PcSystem> value = entry.getValue();
groupMap1.put(entry.getKey(),value.stream().sorted(Comparator.comparing(PcSystem::getUserId).reversed()).collect(Collectors.toList()));
}
for(Map.Entry<String, List<PcSystem>> entry : groupMap2.entrySet()){
List<PcSystem> value = entry.getValue();
groupMap2.put(entry.getKey(),value.stream().sorted(Comparator.comparing(PcSystem::getUserId).reversed()).collect(Collectors.toList()));
}
System.out.println(groupMap1);
System.out.println(groupMap2);
}
2.输出日志
输出结果1
{11=[PcSystem(id=11, userId=1), PcSystem(id=11, userId=2), PcSystem(id=11, userId=3)],
13=[PcSystem(id=13, userId=10), PcSystem(id=13, userId=7), PcSystem(id=13, userId=8),PcSystem(id=13, userId=9)],
12=[PcSystem(id=12, userId=4), PcSystem(id=12, userId=5), PcSystem(id=12, userId=6)]}
输出结果2
{13=[PcSystem(id=13, userId=9), PcSystem(id=13, userId=8), PcSystem(id=13, userId=7),
PcSystem(id=13, userId=10)],
12=[PcSystem(id=12, userId=6), PcSystem(id=12, userId=5), PcSystem(id=12, userId=4)],
11=[PcSystem(id=11, userId=3), PcSystem(id=11, userId=2), PcSystem(id=11, userId=1)]}
输出结果3
{11=[PcSystem(id=11, userId=3), PcSystem(id=11, userId=2), PcSystem(id=11, userId=1)],
13=[PcSystem(id=13, userId=9), PcSystem(id=13, userId=8), PcSystem(id=13, userId=7),
PcSystem(id=13, userId=10)],
12=[PcSystem(id=12, userId=6), PcSystem(id=12, userId=5), PcSystem(id=12, userId=4)]}
输出结果4
{13=[PcSystem(id=13, userId=9), PcSystem(id=13, userId=8), PcSystem(id=13, userId=7),PcSystem(id=13, userId=10)],
12=[PcSystem(id=12, userId=6), PcSystem(id=12, userId=5), PcSystem(id=12, userId=4)],
11=[PcSystem(id=11, userId=3), PcSystem(id=11, userId=2), PcSystem(id=11, userId=1)]}
仅为笔记!仅为笔记!仅为笔记!仅为笔记!