19

I have list for each element I would like to do this (using Java 8):

disabledUsersOnLDAP.stream().forEach(user -> usersRepository
                .findEnabledByUsername(user.getUserName()).setEnabled(false));

How ever usersRepository.findEnabledByUsername might return null.

Of course I can do this instead:

disabledUsersOnLDAP.stream().forEach(user -> {
    UserEntity userEntity = usersRepository.findEnabledByUsername(user.getUserName()); 
    {
        if (userEntity != null) {
            userEntity.setEnabled(false);
        }
    }
});

But I wonder if I could do the null check inline (in the first option)?

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
rayman
  • 20,786
  • 45
  • 148
  • 246
  • Did you consider the new type [`Optional`](http://docs.oracle.com/javase/8/docs/api/java/util/Optional.html)? If something might return _null_, you better return an empty optional instead. Filtering on non-empty optionals is also very easy. – Seelenvirtuose Jul 16 '14 at 11:51

2 Answers2

47

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));
assylias
  • 321,522
  • 82
  • 660
  • 783
  • One more thing. if in the last line - in the the foreach clauses ill want to do more logic how can you modify it? let's say after setEnable I want to send each userEntity to method doSomething(). – rayman Jul 16 '14 at 12:58
  • 3
    `.forEach(userEntity -> { userEntity.setEnabled(false); soSomething(userEntity); });` – assylias Jul 16 '14 at 13:18
7

Some alternatives to the @assylias answser.

Use a method reference to Objects==nonNull for the null check:

disabledUsersOnLDAP.stream()
    .map(User::getUsername)
    .map(usersRepository::findEnabledByUsername)
    .filter(Objects::nonNull)
    .forEach(userEntity -> userEntity.setEnabled(false));

if you can update UserEntity with a disable method

class UserEntity {
    public void disable() {
        setEnabled(false);
    }
}

you can again use a method reference (UserEntity::disable) :

disabledUsersOnLDAP.stream()
    .map(User::getUsername)
    .map(usersRepository::findEnabledByUsername)
    .filter(Objects::nonNull)
    .forEach(UserEntity::disable);

Some resources:

Community
  • 1
  • 1
gontard
  • 28,720
  • 11
  • 94
  • 117
  • And if I would like to add to the last forEach another method call. how would you combine that in? – rayman Jul 16 '14 at 13:01
  • Could you give an example ? – gontard Jul 16 '14 at 13:03
  • disabledUsersOnLDAP.stream() .map(UserLdap::getUserName) .map(usersRepository::findEnabledByUsername) .filter(userEntity -> userEntity != null) .forEach(userEntity -> { userEntity.disable(); cleanUserTicket(userEntity.getUsername()); }); – rayman Jul 16 '14 at 13:11
  • @rayman Ok. Unless you have a specific method doing all this stuff you can't use a method reference. It is important to note that this method is not necessarily on UserEntity ; it could be a static method or a method of an accessible object. The advantage of using method reference is that you reference a named method, the intention is more clear. – gontard Jul 16 '14 at 13:24