public Collection<? extends GrantedAuthority> getAuthorities() {
return getRoles().stream()
.map(role -> new SimpleGrantedAuthority(role.getRole().name()))
.collect(Collectors.toList());
}
//我想在java1.7的代码中不使用lambda表达式来转换这些
假设getRoles()
返回List<Role>
,则结果如下:
List<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
for (Role role : getRoles()) {
simpleGrantedAuthorities.add(new SimpleGrantedAuthority(role.getRole().name()));
}
return simpleGrantedAuthorities;