使用stream对集合进行排序

根据Hash值排序的,无序排序的

List<ModuleVo> moduleList=new ArrayList();
Map<Integer, List<ModuleVo>> collectResult = moduleList.stream().collect(Collectors.groupingBy(ModuleVo::getProjectId));

根据time进行升序排序,如下

TreeMap<Integer, List<ModuleVo>> collectResult  = moduleList.stream().collect(Collectors.groupingBy(ModuleVo::getTime,TreeMap::new,Collectors.toList()));

TreeMap默认按照key升序排序

TreeMap<Integer, List<ModuleVo>> collectResult  = moduleList.stream().collect(Collectors.groupingBy(ModuleVo::getTime,TreeMap::new,Collectors.toList())).descendingMap();
posted @ 2024-07-24 22:33  vickcan  阅读(37)  评论(0)    收藏  举报