Java8 - Methods in the Optional class

Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 05:37:28 Viewed : 237


The Optional class in Java provides several methods to work with optional values. These methods help handle the presence or absence of a value in a more functional and expressive way. Here are some key methods of the Optional class:

1. of:

  • Description:
    • Creates an Optional with a non-null value.
  • Syntax:
    • Optional<T> of(T value)
  • Example:
    java
    Optional<String> optional = Optional.of("Hello, World!");

2. ofNullable:

  • Description:
    • Creates an Optional with a given value, which can be null.
  • Syntax:
    • Optional<T> ofNullable(T value)
  • Example:
    java
    String nullableValue = /* some value, possibly null */; Optional<String> optional = Optional.ofNullable(nullableValue);

3. empty:

  • Description:
    • Creates an empty Optional.
  • Syntax:
    • Optional<T> empty()
  • Example:
    java
    Optional<String> emptyOptional = Optional.empty();

4. isPresent:

  • Description:
    • Returns true if the Optional contains a value; otherwise, returns false.
  • Syntax:
    • boolean isPresent()
  • Example:
    java
    Optional<String> optional = /* some optional */; if (optional.isPresent()) { // Value is present String value = optional.get(); // ... } else { // Value is absent // ... }

5. ifPresent:

  • Description:
    • Executes the specified consumer if a value is present.
  • Syntax:
    • void ifPresent(Consumer<? super T> consumer)
  • Example:
    java
    Optional<String> optional = /* some optional */; String result = optional.orElse("Default Value");

6. orElse:

  • Description:
    • Returns the value if present, otherwise returns the specified default value.
  • Syntax:
    • T orElse(T other)
  • Example:
    java
    Optional<String> optional = /* some optional */; String result = optional.orElseGet(() -> computeDefaultValue());

7. orElseGet:

  • Description:
    • Returns the value if present, otherwise returns the result of invoking the supplier function.
  • Syntax:
    • T orElseGet(Supplier<? extends T> other)
  • Example:
    java
    Optional<String> optional = /* some optional */; String result = optional.orElseGet(() -> computeDefaultValue());

8. orElseThrow:

  • Description:
    • Returns the value if present, otherwise throws an exception produced by the provided supplier.
  • Syntax:
    • <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X
  • Example:
    java
    Optional<String> optional = /* some optional */; String result = optional.orElseThrow(() -> new RuntimeException("No value present"));

9. filter:

  • Description:
    • If a value is present, applies the given predicate to it and returns an Optional describing the value, if the predicate is true, otherwise returns an empty Optional.
  • Syntax:
    • Optional<T> filter(Predicate<? super T> predicate)
  • Example:
    java
    Optional<String> optional = /* some optional */; Optional<String> filtered = optional.filter(value -> value.startsWith("H"));

10. map:

  • Description:
    • If a value is present, applies the given function to it and returns an Optional describing the result, otherwise returns an empty Optional.
  • Syntax:
    • <U> Optional<U> map(Function<? super T, ? extends U> mapper)
  • Example:
    java
    Optional<String> optional = /* some optional */; Optional<Integer> length = optional.map(String::length);

11. flatMap:

  • Description:
    • If a value is present, applies the given function to it and returns the result, otherwise returns an empty Optional.
  • Syntax:
    • <U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper)
  • Example:
    java
    Optional<String> optional = /* some optional */; Optional<Character> firstChar = optional.flatMap(value -> value.isEmpty() ? Optional.empty() : Optional.of(value.charAt(0)));

These are some of the essential methods provided by the Optional class. They help in writing more expressive and concise code when dealing with optional values, avoiding explicit null checks and promoting better error handling practices.

Search
Related Articles

Leave a Comment: