Optional or() method in Java with examples Last Updated : 30 Jul, 2019 Comments Improve Suggest changes Like Article Like Report The or() method of java.util.Optional class in Java is used to get this Optional instance if any value is present. If there is no value present in this Optional instance, then this method returns an Optional instance with the value generated from the specified supplier. Syntax: public Optional<T> or(Supplier<T> supplier) Parameters: This method accepts supplier as a parameter of type T to generate an Optional instance with the value generated from the specified supplier. Return supplier: This method returns this Optional instance, if any value is present. If there is no value present in this Optional instance, then this method returns an Optional instance with the value generated from the specified supplier. Exception: This method throws NullPointerException if the supplying function is null or produces a null result. Below programs illustrate or() method: Note: As this method was added in Java 9, the programs need JDK 9 to execute. Program 1: Java // Java program to demonstrate // Optional.or() method import java.util.*; import java.util.function.*; public class GFG { public static void main(String[] args) { // create a Optional Optional<Integer> op = Optional.of(9455); // print supplier System.out.println("Optional: " + op); // or supplier System.out.println("Optional by or(() ->" + " Optional.of(100)) method: " + op.or(() -> Optional.of(100))); } } Output: Optional: Optional[9455] Optional by or(() -> Optional.of(100)) method: Optional[9455] Program 2: Java // Java program to demonstrate // Optional.or() method import java.util.*; import java.util.function.*; public class GFG { public static void main(String[] args) { // create a Optional Optional<Integer> op = Optional.empty(); // print supplier System.out.println("Optional: " + op); try { // or supplier System.out.println("Optional by or(() ->" + " Optional.of(100)) method: " + op.or(() -> Optional.of(100))); } catch (Exception e) { System.out.println(e); } } } Output: Optional: Optional.empty Optional by or(() -> Optional.of(100)) method: Optional[100] Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Optional.html#or-java.util.function.Supplier- Comment More infoAdvertise with us Next Article Optional or() method in Java with examples S ShubhamMaurya3 Follow Improve Article Tags : Java Java - util package Java-Functions Java-Optional Practice Tags : Java Similar Reads Optional orElse() method in Java with examples The orElse() method of java.util.Optional class in Java is used to get the value of this Optional instance, if present. If there is no value present in this Optional instance, then this method returns the specified value. Syntax: public T orElse(T value) Parameters: This method accepts value as a pa 2 min read Optional stream() method in Java with examples The stream() method of java.util.Optional class in Java is used to get the sequential stream of the only value present in this Optional instance. If there is no value present in this Optional instance, then this method returns returns an empty Stream. Syntax: public Stream<T> stream() Paramete 2 min read Optional toString() method in Java with examples The toString() method of java.util.Optional class in Java is used to get the string representation of this Optional instance. Syntax: public String toString() Parameters: This method accepts nothing. Return value: This method returns the string representation of this Optional instance. Below program 1 min read Optional of() method in Java with examples The of() method of java.util.Optional class in Java is used to get an instance of this Optional class with the specified value of the specified type. Syntax: public static <T> Optional<T> of(T value) Parameters: This method accepts value as parameter of type T to create an Optional insta 1 min read Optional get() method in Java with examples The get() method of java.util.Optional class in Java is used to get the value of this Optional instance. If there is no value present in this Optional instance, then this method throws NullPointerException. Syntax: public T get() Parameters: This method do not accept any parameter. Return value: Thi 2 min read OptionalInt toString() method in Java with examples The toString() method help us to get a non-empty string representation of this OptionalInt.This non-empty string representation is suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions. Syntax: public String toString() Parameters: This 1 min read Optional orElseThrow() method in Java with examples The orElseThrow() method of java.util.Optional class in Java is used to get the value of this Optional instance if present. If there is no value present in this Optional instance, then this method throws the exception generated from the specified supplier. Syntax: public <X> T orElseThrow(Supp 2 min read OptionalInt stream() method in Java with examples The stream() method help us to get value contain by OptionalInt as IntStream. If a value is present, method returns a sequential IntStream containing only that value, otherwise returns an empty IntStream. Syntax: public IntStream stream() Parameters: This method accepts nothing. Return value: This m 1 min read Optional empty() method in Java with examples The empty() method of java.util.Optional class in Java is used to get an empty instance of this Optional class. This instance do not contain any value. Syntax: public static <T> Optional<T> empty() Parameters: This method accepts nothing. Return value: This method returns an empty instan 1 min read OptionalInt orElseGet() method in Java with examples The orElseGet(java.util.function.IntSupplier) method helps us to get the value in this OptionalInt object. If a value is not present in this OptionalInt, then this method returns the result produced by the supplying function, passed as the parameter Syntax: public int orElseGet(IntSupplier supplier) 2 min read Like