I have list for each element I would like to do this (using Java 8):
disabledUsersOnLDAP.stream().forEach(user -> usersRepository
.findEnabledB
You could do a mapping + filtering before running the forEach
part, which also makes it a bit more readable:
disabledUsersOnLDAP.stream()
.map(user -> usersRepository.findEnabledByUsername(user.getUserName()))
.filter(userEntity -> userEntity != null)
.forEach(userEntity -> userEntity.setEnabled(false));
Or as an alternative:
disabledUsersOnLDAP.stream()
.map(User::getUsername)
.map(usersRepository::findEnabledByUsername)
.filter(Objects::nonNull)
.forEach(userEntity -> userEntity.setEnabled(false));