r/java • • Feb 17 '26

Objects.requireNonNullElse

I must have been living in a cave. I just discovered that this exists.
I can code

City city = Objects.requireNonNullElse(form.getCity(), defaultCity);

... instead of:

City city = form.getCity();

if(city == null){

city = defaultCity;

}

113 Upvotes

136 comments sorted by

View all comments

Show parent comment

4

u/narrow-adventure Feb 18 '26

I agree, I think that verbosity has totally increased.

On top of the new functions being quite lengthy, I think that people chaining too many stream/optional functions makes it really hard to read code. Like 3-4 short chains is fine, but if it goes up to 10 it just becomes unreadable for me.