I am trying to convert a nested for loop to java 8 streams.
Loop implementation
private Set<String> getAllowRolesFromInheritedPolicy(String userId, List<Policy> allowedPoliciesThisCustomer) {
Set<String> allowedRolesThisUser = Sets.newHashSet();
for (Policy policy : allowedPoliciesThisCustomer) {
Map<String, Role> roles = policy.getRoles();
for (Role role : roles.values()) {
if (role.getUsers().contains(userId)) {
allowedRolesThisUser.add(role.getRoleName());
}
}
}
In the code role.getUsers() returns List<String>.
Stream Implementation
I am trying to change the for loop into java 8 streams :
Set<String> allowedRolesThisUser = Sets.newHashSet(allowedPoliciesThisCustomer.stream()
.map(policy -> policy.getRoles().values())
.filter(role -> role.getUsers().contains(userId))
.collect(Collectors.toList()));
But compiler says:
error: cannot find symbol : .filter(role -> role.getUsers().contains(userId))
symbol: method getUsers(), location: variable role of type Collection<Role>
Role has the function getUsers, Collection<Role> does not. What should I do to make this conversion correct?
Thank you.