Module java.base
Package java.util

# Class Optional

≠
public final class Optional<T>
extends java.lang.Object
Comparing jdk-master and valhalla-csr-open

Type Parameters:
  `T` - the type of value

public final class Optional<T> extends Object
A container object which may or may not contain a non-`null` value. If a value is present, `isPresent()` returns `true` . If no value is present, the object is considered empty and `isPresent()` returns `false` .

Additional methods that depend on the presence or absence of a contained value are provided, such as `orElse()` (returns a default value if no value is present) and `ifPresent()` (performs an action if a value is present).

This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail mutexes, or with object references.

When preview features are enabled, `Optional` is a value classPREVIEW . Use of value class instances for synchronization, mutexes, or with object references result in `IdentityException` PREVIEW Moved out of a paragraph.Moved to a division with class preview-block.Moved to a division with class preview-comment..

API Note:
  `Optional` is primarily intended for use as a method return type where there is a clear need to represent "no result," and where using `null` is likely to cause errors. A variable whose type is `Optional` should never itself be `null` ; it should always point to an `Optional` instance.
Since:
  1.8

## Methods

- =
  public static <T> java.util.Optional<T> empty()
- =
  public boolean equals(java.lang.Object arg0)
- =
  public java.util.Optional<T> filter(java.util.function.Predicate<? super T> arg0)
- =
  public <U> java.util.Optional<U> flatMap(java.util.function.Function<? super T, ? extends java.util.Optional<? extends U>> arg0)
- =
  public T get()
- =
  public int hashCode()
- =
  public void ifPresent(java.util.function.Consumer<? super T> arg0)
- =
  public void ifPresentOrElse(java.util.function.Consumer<? super T> arg0, java.lang.Runnable arg1)
- =
  public boolean isEmpty()
- =
  public boolean isPresent()
- =
  public <U> java.util.Optional<U> map(java.util.function.Function<? super T, ? extends U> arg0)
- =
  public static <T> java.util.Optional<T> of(T arg0)
- =
  public static <T> java.util.Optional<T> ofNullable(T arg0)
- =
  public java.util.Optional<T> or(java.util.function.Supplier<? extends java.util.Optional<? extends T>> arg0)
- =
  public T orElse(T arg0)
- =
  public T orElseGet(java.util.function.Supplier<? extends T> arg0)
- =
  public T orElseThrow()
- =
  public <X> T orElseThrow(java.util.function.Supplier<? extends X> arg0) throws X
- =
  public java.util.stream.Stream<T> stream()
- =
  public java.lang.String toString()

## Summary

Differences
| Elements | Comments | Descriptions | Total
Added | Changed | Removed | Added | Changed | Removed | Added | Changed | Removed
--- | --- | --- | --- | --- | --- | --- | --- | ---
Optional |  |  |  |  |  |  | 7 | 2 |  | 9
Total |  |  |  |  |  |  | 7 | 2 |  | 9