Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite double nested for loop as a Java 8 stream

I have the following Java method:

public List<GrantedAuthority> toAuthorities(Set<Role> roles) {
    List<GrantedAuthority> authorities = new ArrayList<>();

    if (null != roles) {
        for (Role role : roles) {
            for (Permission permission : role.getPermissions()) {
                authorities.add(new SimpleGrantedAuthority("ROLE_" + permission.getLabel()));
            }
        }
    }

    return authorities;
}

I'm trying to rewrite it using Java 8 streams. My best attempt thus far:

public List<GrantedAuthority> toAuthorities(Set<Role> roles) {
    List<GrantedAuthority> authorities = new ArrayList<>();

    if (null != roles) {
        roles.stream().filter(role -> ???).collect(Collectors.toList());
    }

    return authorities;
}

But I'm at a loss as to what I put in the stream filter (substituting ???)...any ideas?

like image 855
hotmeatballsoup Avatar asked Oct 25 '18 01:10

hotmeatballsoup


2 Answers

You can do it using flatMap and map instaead as :

if (null != roles) {
    authorities = roles.stream()
         .flatMap(role -> role.getPermissions().stream()) // Stream<Permission>
         .map(permission -> 
                 new SimpleGrantedAuthority("ROLE_" + permission.getLabel())) // Stream<SimpleGrantedAuthority>
         .collect(Collectors.toList());
}

In the for loop code, you are not filtering out/in any iteration based on a condition and iterating throughout the lists, hence you don't require a filter here.


And using the above your complete method could be written as :

public List<GrantedAuthority> toAuthorities(Set<Role> roles) {
    return roles == null ? new ArrayList<>() : roles.stream()
            .flatMap(role -> role.getPermissions().stream())
            .map(permission -> new SimpleGrantedAuthority("ROLE_" + permission.getLabel()))
            .collect(Collectors.toList());
}

Or as suggested by shmosel, with method references this could be transformed as :

return roles == null ? new ArrayList<>() : roles.stream()
        .map(Role::getPermissions)
        .flatMap(Collection::stream)
        .map(Permission::getLabel)
        .map("ROLE_"::concat)
        .map(SimpleGrantedAuthority::new)
        .collect(Collectors.toList());
like image 125
Naman Avatar answered Sep 30 '22 06:09

Naman


You could do it in a single chain, not sure how readable that is to you though:

public static List<GrantedAuthority> toAuthorities(Set<Role> roles) {
    return Optional.ofNullable(roles)
            .orElse(Collections.emptySet())
            .stream()
            .flatMap(r -> r.getPermissions().stream())
            .map(Permission::getLabel)
            .map("ROLE_"::concat)
            .map(SimpleGrantedAuthority::new)
            .collect(Collectors.toList());
}
like image 33
Eugene Avatar answered Sep 30 '22 07:09

Eugene