Querying an Optional

The presence of a value in an Optional can be determined by the isPresent() method, and the value can be obtained by calling the get() method—which is not much better than checking explicitly for the null value, but as we shall see, other methods in the Optional class alleviate this drudgery. The get() method throws a NoSuchElementException if there is no value in the Optional.

Click here to view code image

if (book0.getOptBlurb().isPresent()) {
  System.out.println(book0.getOptBlurb().get());    // Java Programmers tell all!
}
System.out.println(book1.getOptBlurb().get());      // NoSuchElementException

The idiom of determining the presence of a value and then handling the value is combined by the ifPresent() method that accepts a Consumer to handle the value if one is present. The ifPresent() method does nothing if there is no value present in the Optional.

Click here to view code image

book0.getOptBlurb().ifPresent(System.out::println); //Java Programmers tell all!
T get()

If a value is present in this Optional, the method returns that value; otherwise, it throws a NoSuchElementException.

Click here to view code image

boolean isPresent()
void ifPresent(Consumer<? super T> consumer)

The first method returns true if there is a value present in this Optional; otherwise, it returns false.

If a value is present in this Optional, the second method invokes the specified consumer with the value; otherwise, it does nothing.

Click here to view code image

T orElse(T other)
T orElseGet(Supplier<? extends T> other)
<X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)
                        throws X extends Throwable

If a value is present in this Optional, all three methods return this value.

They differ in their action when a value is not present in this Optional. The first method returns the other value. The second method invokes the specified supplier other and returns its result. The third method invokes the specified supplier exceptionSupplier and throws the exception created by this supplier.

Note that the type of argument in the first two methods must be compatible with the parameterized type of the Optional on which the method is invoked, or the compiler will issue an error.

Often, a default value should be supplied when an Optional does not contain a value. The orElse() method returns the value in the Optional if one is present; otherwise, it returns the value given by the argument specified in the method call.

The orElse() method in the statement below returns the blurb in the book referenced by the reference book0, as this book has a blurb.

Click here to view code image

String blurb = book0.getOptBlurb()
                    .orElse(“No blurb”);           // “Java Programmers tell all!”

The book referenced by the reference book1 has no blurb. Therefore, the orElse() method invoked on the optional blurb returns the argument in the method.

Click here to view code image

blurb = book1.getOptBlurb().orElse(“No blurb”);    // “No blurb”

For an Optional with a value, the orElseGet() method returns the value in the Optional. The orElseGet() method in the statement below returns the object supplied by the Supplier specified as an argument, since the book has no blurb.

Click here to view code image

blurb = book1.getOptBlurb().orElseGet(() -> “No blurb”); // “No blurb”

For an Optional with a value, the orElseThrow() method also returns the value in the Optional. The orElseThrow() method in the statement below throws the exception created by the Supplier specified as an argument, since the book has no blurb.

Click here to view code image

blurb = book1.getOptBlurb()                        // RuntimeException
             .orElseThrow(() -> new RuntimeException(“No blurb”));

Leave a Reply

Your email address will not be published. Required fields are marked *