Hello fellow Java developers,
I know the subject may be a bit in advance
as the JDK8 is not yet released (and not for now anyway..) but I was reading som
Although the answers are 100% correct, a small suggestion to improve null
case handling of the list itself with Optional:
List listOfStuffFiltered = Optional.ofNullable(listOfStuff)
.orElseGet(Collections::emptyList)
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
The part Optional.ofNullable(listOfStuff).orElseGet(Collections::emptyList)
will allow you to handle nicely the case when listOfStuff
is null and return an emptyList instead of failing with NullPointerException.