I have some data in input that I'll have to use to set all properties of a POJO. The POJO might be partially set. My problem is to set the property only if related input data is not null. I know I can do this in two ways:
if (input != null) {
obj.setData(input);
}
or
obj.setData(input != null ? input : obj.getData());
I'm looking for a solution less ugly and better for objects with a big number of properties to set.
You can also use java8
Optional
obj.setData(Optional.ofNullable(input).orElse(obj.getData()));
Or even use a more elegant way:
Optional.ofNullable(input).ifPresent(obj::setData);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With