Java这段代码如何优化?List 对象和foreach有点多
publicCollection<String>getTypes(@PathVariablelongid){List<UserGroup>userGroups=this....
public Collection<String> getTypes(@PathVariable long id) {
List<UserGroup> userGroups = this.userGroupRepository.findAllByUserId(id);
if (userGroups==null){
return null;
}
List<Long> gids = userGroups.stream().map(UserGroup::getGroupId).collect(Collectors.toList());
List<GroupRole> groupRoles = this.groupRoleRepository.findAllByGroupIdIn(gids);
if (groupRoles==null){
return null;
}
List<Long> rids = groupRoles.stream().map(GroupRole::getRoleId).collect(Collectors.toList());
List<Role> roles = this.roleRepository.findAllByIdIn(rids);
List<Long> resIds = new ArrayList<>();
for (Role role : roles) {
Collection<Privilege> ps = role.getPrivileges().values();
for (Privilege p:ps) {
if(PrivilegeType.VALUE.equals(p.getResId())) {
resIds.add(p.getResId());
}
}
}
List<ResValue> resValues = this.resValueRepository.findAllByIdIn(resIds);
Set<String> values = new HashSet<>();
for (ResValue resValue: resValues ) {
values.add(resValue.getValue());
}
return values;
} 展开
List<UserGroup> userGroups = this.userGroupRepository.findAllByUserId(id);
if (userGroups==null){
return null;
}
List<Long> gids = userGroups.stream().map(UserGroup::getGroupId).collect(Collectors.toList());
List<GroupRole> groupRoles = this.groupRoleRepository.findAllByGroupIdIn(gids);
if (groupRoles==null){
return null;
}
List<Long> rids = groupRoles.stream().map(GroupRole::getRoleId).collect(Collectors.toList());
List<Role> roles = this.roleRepository.findAllByIdIn(rids);
List<Long> resIds = new ArrayList<>();
for (Role role : roles) {
Collection<Privilege> ps = role.getPrivileges().values();
for (Privilege p:ps) {
if(PrivilegeType.VALUE.equals(p.getResId())) {
resIds.add(p.getResId());
}
}
}
List<ResValue> resValues = this.resValueRepository.findAllByIdIn(resIds);
Set<String> values = new HashSet<>();
for (ResValue resValue: resValues ) {
values.add(resValue.getValue());
}
return values;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询