1.使用Comparator 来排序一个list
sorted(Comparator.comparingInt(SysDept::getSort))
// 查询全部部门
List<SysDept> deptAllList = deptMapper.selectList(Wrappers.emptyWrapper());
// 查询数据权限内部门
List<Integer> deptOwnIdList = deptMapper.selectListByScope(Wrappers.emptyWrapper(), new DataScope()).stream()
.map(SysDept::getDeptId).collect(Collectors.toList());
// 权限内部门
List<TreeNode<Integer>> collect = deptAllList.stream()
.filter(dept -> dept.getDeptId().intValue() != dept.getParentId())
.sorted(Comparator.comparingInt(SysDept::getSort)).map(dept -> {
TreeNode<Integer> treeNode = new TreeNode();
treeNode.setId(dept.getDeptId());
treeNode.setParentId(dept.getParentId());
treeNode.setName(dept.getName());
// 有权限不返回标识
treeNode.setExtra(MapUtil.of("isLock", !deptOwnIdList.contains(dept.getDeptId())));
return treeNode;
}).collect(Collectors.toList());
2.切割一个字符串,然后收集成list
List<SysRoleMenu> roleMenuList = Arrays.stream(menuIds.split(",")).map(menuId -> {
SysRoleMenu roleMenu = new SysRoleMenu();
roleMenu.setRoleId(roleId);
roleMenu.setMenuId(Integer.valueOf(menuId));
return roleMenu;
}).collect(Collectors.toList());
3.获取指定字段
List<String> nameList = equipmentService.lambdaQuery().select(Equipment::getEquipmentName)
.list().stream().map(Equipment::getEquipmentName)
.collect(Collectors.toList());
4.根据某个字段去重
// 去掉重复的部门
List<Long> collect = list.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<IssuedUser>(comparing(IssuedUser::getExecuteDetachmentId))), ArrayList::new)) // 去重
.stream().map(o -> o.getExecuteDetachmentId()).collect(Collectors.toList());