Type an orElseGet with a lazy supplier
Type an orElseGet with a lazy supplier
Answer
String name = optName.orElseGet(() -> fetchDefault());
orElseGet accepts a Supplier and only evaluates it when the Optional is empty. Prefer it over orElse when the fallback is expensive to compute.