Bootstrap

Map 分组后根据key排序

原数据

List<Map<String, Object>> listData = userDao.getUserList(user);

1.分组

Map<String, List<Map<String, Object>>> groupByDatas = listData.stream().collect(
                Collectors.groupingBy(map -> String.valueOf(map.get("name"))));

2.排序

//排序
Map<String, List<Map<String, Object>>> result=groupByDatas.entrySet().stream().sorted((o1, o2) -> {
            try {
                String d1 = String.valueOf(o1.getKey());
                String d2 = String.valueOf(o2.getKey());
                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
        ));

3.拓展 List<User> 根据实体属性排序

3.1单个属性排序

List<UserVo> users=UserMapper.queryUsers(userDto);
users.sort((UserVo u1,UserVo u2)->u2.getAge() - u1.getAge());

3.2多个属性排序

List<UserVo> users=UserMapper.queryUsers(userDto);
Comparator<UserVo> comparator1 = (UserVo u1,UserVo u2)->u2.getAge() - u1.getAge();
Comparator<UserVo> comparator2 = comparator1.thenComparing((UserVo u1,UserVo u2) -> u2.getNumber() - u1.getNumber());
users.sort(comparator2);

;