我想知道如何将以下 lambda 表达式转换为 for-each loop:for java1.7 版本

I would like to know how do I convert the following lambda expression to a for-each loop:for java1.7 version

public static UserPrinciple build(User user) {
    List<GrantedAuthority> authorities = user.getRoles().stream().map(role ->
            new SimpleGrantedAuthority(role.getName().name())
    ).collect(Collectors.toList());

    return new UserPrinciple(
            user.getId(),
            user.getName(),
            user.getUsername(),
            user.getEmail(),
            user.getPassword(),
            authorities
    );
}

我想知道如何将以下 lambda 表达式转换为 java 7.

的 for-each 循环

只需使用普通的 for 循环:

List<GrantedAuthority> authorities  = new ArrayList<GrantedAuthority>();
for (Role role : user.getRoles()) {
    authorities.add(new SimpleGrantedAuthority(role.getName().name()));
}

我不知道 role 的具体语法或属性,但这是将 lambda 转换为 for 循环的方式。

public static UserPrinciple build(User user) {
    List<GrantedAuthority> authorities = new ArrayList<>();

    for (Role role : user.getRoles()) {
        authorities.add(new SimpleGrantedAuthority(role.getName().name());
    }

    return new UserPrinciple(
            user.getId(),
            user.getName(),
            user.getUsername(),
            user.getEmail(),
            user.getPassword(),
            authorities
    );
}