java.stream流使用

1、java.stream流使用

/* 
 */ 
List<TRoleUser> roleUserList = new ArrayList<>();
// 1.分组;将所有相同projectId的用户所有角色ids分组映射 
Map<String, List<String>> projectId_roleIds_map = roleUserList.stream().collect(Collectors.groupingBy(IRoleUser::getProjectId, Collectors.mapping(TRoleUser::getRoleId, Collectors.toList())));
// 2.分组;将所有相同projectId的用户所有角色s分组映射 
Map<String, List<TRoleUser>> projectId_roles_map = roleUserList.stream().collect(Collectors.groupingBy(IRoleUser::getProjectId, Collectors.toList()));
// 3.将roleUserList中所有的角色id抽取出来封装List集合中 
List<String> roleIds = roleUserList.stream().map(TRoleUser::getRoleId).collect(Collectors.toList());
 
/* 
 */
List<ProjectInfo> projectInfoList = new ArrayList<>();
// 4.项目id与项目信息映射,map遇到key相同时新值替换旧值 
Map<String, ProjectInfo> id_info_map = projectInfoList.stream().collect(Collectors.toMap(ProjectInfo::getId, Function.identity(), (oldKey, newKey) -> newKey));
// 4.2.返回的Map排序与数据源projectInfoList的顺序相同 
Map<String, ProjectInfo> id_info_map = projectInfoList.stream().collect(Collectors.toMap(ProjectInfo::getId, Function.identity(), (oldKey, newKey) -> newKey, TreeMap::new));
// 5.过滤出项目状态'可用'的项目列表 
List<ProjectInfo> infos = projectInfoList.stream().filter(projectInfo -> "ENABLE".equals(projectInfo.getStatus())).collect(Collectors.toList());

你可能感兴趣的:(技术)