##In Java 9, some static methods have been added to the
Optional
Optional.or()method returns anOptionaldescribing a value that is returned if present, otherwise an Optional generated by the provided function.
Syntaxpublic Optionalor(Supplier extends Optional extends T>> supplier)
import java.util.Optional; import java.util.function.Supplier; public class OptionalOrTest { public static void main(String args[]) { Optionaloptional = Optional.of("TutorialsPoint"); Supplier > supplierString = () -> Optional.of("Not Present"); optional = optional.or(supplierString); optional.ifPresent(x -> System.out.println("Value: " + x)); optional = Optional.empty(); optional = optional.or(supplierString); optional.ifPresent(x -> System.out.println("Value: " + x)); } }
Value: TutorialsPoint Value: Not Present
The above is the detailed content of What is the importance of Optional.or() method in Java 9?. For more information, please follow other related articles on the PHP Chinese website!