What is the best/idiomatic way of doing a null check before getting a stream?
I have method that is receiving a List that might be null. So I can\'t jus
List
The best thing I can think of would be to use an Optional with the orElseGet method.
Optional
orElseGet
return Optional.ofNullable(userList) .orElseGet(Collections::emptyList) .stream() .map(user -> user.getName()) .collect(toList());
Updated with @Misha's suggest to use Collections::emptyList over ArrayList::new
Collections::emptyList
ArrayList::new