MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/java/comments/1r7iv5p/objectsrequirenonnullelse/o5xxqcw/
r/java • u/edurbs • Feb 17 '26
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;
}
136 comments sorted by
View all comments
Show parent comment
8
There's also Objects::requireNonNullElseGet
Objects::requireNonNullElseGet
1 u/metaquine Feb 19 '26 That's the winner
1
That's the winner
View all comments
Show parent comment
8
u/_INTER_ Feb 17 '26
There's also
Objects::requireNonNullElseGet