Is there a "nice" way to create a copy of an Optional object?
For example, say I have a method
Optional<Obj> doSomeStuff(Optional<Obj> input){
if(something){
return Optional.copyOf(input); // does this exist in some Guava library or something?
}
// ...
}
Basically, I want to have immutability so that I don't pass the same Optional around, and if something gets triggered then I want to make sure that I create a brand new Optional with same contents of the input Optional (whether it be empty or not).
Is there any clean way to do it? The Optional.copyOf method does not exist.
The Optional class is immutable, so you do not need copy,. You can just do return input.
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