Class Optional<T>


  • public class Optional<T>
    extends Object
    • Method Detail

      • empty

        public static <T> Optional<T> empty()
        Returns an empty Optional instance. No value is present for this Optional.
      • of

        public static <T> Optional<T> of​(@NonNull
                                         T value)
        Returns an Optional with the specified present non-null value.
        Parameters:
        value - the value to be present, which must be non-null
        Returns:
        an Optional with the value present
      • ofNullable

        public static <T> Optional<T> ofNullable​(T value)
        Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
        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
      • get

        public 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
      • isPresent

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

        public 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: