Interface Optional<T>


public interface Optional<T>
A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value.

Additional methods that depend on the presence or absence of a contained value are provided, such as orElse(Object) (return a default value if value not present).

  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Optional<T>
    Returns an empty Optional instance.
    get()
    If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
    boolean
    Return true if there is a value present, otherwise false.
    static <T> Optional<T>
    of(T value)
    Returns an Optional with the specified present non-null value.
    static <T> Optional<T>
    ofNullable(T value)
    Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
    orElse(T other)
    Return the value if present, otherwise return other.
  • Method Details

    • get

      T get()
      If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
      Returns:
      the non-null value held by this Optional
      Throws:
      NoSuchElementException - if there is no value present
      See Also:
    • isPresent

      boolean isPresent()
      Return true if there is a value present, otherwise false.
      Returns:
      true if there is a value present, otherwise false
    • orElse

      T orElse(T other)
      Return the value if present, otherwise return other.
      Parameters:
      other - the value to be returned if there is no value present, may be null
      Returns:
      the value, if present, otherwise other
    • empty

      static <T> Optional<T> empty()
      Returns an empty Optional instance. No value is present for this Optional. Though it may be tempting to do so, avoid testing if an object is empty by comparing with == against instances returned by Option.empty(). There is no guarantee that it is a singleton. Instead, use isPresent().
      Type Parameters:
      T - Type of the non-existent value
      Returns:
      an empty Optional
    • of

      static <T> Optional<T> of(T value)
      Returns an Optional with the specified present non-null value.
      Type Parameters:
      T - the class of the value
      Parameters:
      value - the value to be present, which must be non-null
      Returns:
      an Optional with the value present
      Throws:
      NullPointerException - if value is null
    • ofNullable

      static <T> Optional<T> ofNullable(T value)
      Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
      Type Parameters:
      T - the class of the value
      Parameters:
      value - the possibly-null value to describe
      Returns:
      an Optional with a present value if the specified value is non-null, otherwise an empty Optional