r/java • u/edurbs • 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
View all comments
Show parent comment
7
u/OwnBreakfast1114 Feb 18 '26
Sealed interface switches are a direct replacement for the visitor pattern, so it just depends on how many places you have where you have a small set of types and a lot of operations on those types.