将部分代码重构为lambda表达式

时间:2019-03-07 10:21:06

标签: java lambda refactoring

我有此代码:

    for (UserDTO usersList:userRepo.getContent()) {
        Set<String> str2 = new HashSet<>();
        for (String authority:usersList.getAuthorities()) {
            if(!authority.equals(AuthoritiesConstants.ADMIN) && !authority.equals(AuthoritiesConstants.USER)){
                str2.add(authority);
            }
        }
        usersList.getAuthorities().removeAll(str2);
    }

是否可以将其简化为lambda表达式?

1 个答案:

答案 0 :(得分:4)

您似乎在寻找removeIf为:

userRepo.getContent().forEach(usersList -> 
        usersList.getAuthority()
                .removeIf(authority -> 
                        !authority.equals(AuthoritiesConstants.ADMIN) && !authority.equals(AuthoritiesConstants.USER)));