如何使用Java 8 Stream API重构此代码?
for (Role role: user.getRoles()) {
for (Privilege privilege: role.getPrivileges()) {
System.out.println(privilege.getName());
}
}
试着这样:
user.getRoles().stream()
.map(role -> role.getPrivileges())
.forEach(privelege -> System.out.println(privelege.getName()));