I often find myself using this idiom:
AtomicReference<MyCoolObject> coolReference = new AtomicReference(new MyCoolObject());
getOptionalValue().ifPresent(presentValue -> {
coolReference.set(new MyCoolObject(presentValue));
});
return coolReference.get();
Is this a bad habit, or a legitimate use of AtomicReference?
getOptionalValue().map(MyCoolObject::new).ifPresent(coolReference::set)return getOptionalValue().map(MyCoolObject::new).orElseGet(MyCoolObject::new);MyCoolObjectinside the lambda.