Java Language The java.util.Objects Class Objects.nonNull() method reference use in stream api

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

In the old fashion way for collection null check

List<Object> someObjects = methodGetList();
for (Object obj : someObjects) {
    if (obj == null) {
        continue;
    }
    doSomething(obj);
}

With the Objects.nonNull method and Java8 Stream API, we can do the above in this way:

List<Object> someObjects = methodGetList();
someObjects.stream()
           .filter(Objects::nonNull)
           .forEach(this::doSomething);


Got any Java Language Question?