Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Methods](https://developer.android.com/reference/java/util/OptionalDouble#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/util/OptionalDouble#inhmethods)

# OptionalDouble


`
public

final

class
OptionalDouble
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.util.OptionalDouble |

<br />

*** ** * ** ***

A container object which may or may not contain a `double` 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 `https://developer.android.com/reference/java/util/OptionalDouble#orElse(double)`
(returns a default value if no value is present) and
`https://developer.android.com/reference/java/util/OptionalDouble#ifPresent(java.util.function.DoubleConsumer)` (performs
an action if a value is present).

This is a [value-based](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/doc-files/ValueBased.html)
class; programmers should treat instances that are
[equal](https://developer.android.com/reference/java/util/OptionalDouble#equals(java.lang.Object)) as interchangeable and should not
use instances for synchronization, or unpredictable behavior may
occur. For example, in a future release, synchronization may fail.

## Summary

| ### Public methods ||
|---|---|
| ` static https://developer.android.com/reference/java/util/OptionalDouble` | ` https://developer.android.com/reference/java/util/OptionalDouble#empty()() ` Returns an empty `OptionalDouble` instance. |
| ` boolean` | ` https://developer.android.com/reference/java/util/OptionalDouble#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this `OptionalDouble`. |
| ` double` | ` https://developer.android.com/reference/java/util/OptionalDouble#getAsDouble()() ` If a value is present, returns the value, otherwise throws `NoSuchElementException`. |
| ` int` | ` https://developer.android.com/reference/java/util/OptionalDouble#hashCode()() ` Returns the hash code of the value, if present, otherwise `0` (zero) if no value is present. |
| ` void` | ` https://developer.android.com/reference/java/util/OptionalDouble#ifPresent(java.util.function.DoubleConsumer)(https://developer.android.com/reference/java/util/function/DoubleConsumer action) ` If a value is present, performs the given action with the value, otherwise does nothing. |
| ` void` | ` https://developer.android.com/reference/java/util/OptionalDouble#ifPresentOrElse(java.util.function.DoubleConsumer,%20java.lang.Runnable)(https://developer.android.com/reference/java/util/function/DoubleConsumer action, https://developer.android.com/reference/java/lang/Runnable emptyAction) ` If a value is present, performs the given action with the value, otherwise performs the given empty-based action. |
| ` boolean` | ` https://developer.android.com/reference/java/util/OptionalDouble#isEmpty()() ` If a value is not present, returns `true`, otherwise `false`. |
| ` boolean` | ` https://developer.android.com/reference/java/util/OptionalDouble#isPresent()() ` If a value is present, returns `true`, otherwise `false`. |
| ` static https://developer.android.com/reference/java/util/OptionalDouble` | ` https://developer.android.com/reference/java/util/OptionalDouble#of(double)(double value) ` Returns an `OptionalDouble` describing the given value. |
| ` double` | ` https://developer.android.com/reference/java/util/OptionalDouble#orElse(double)(double other) ` If a value is present, returns the value, otherwise returns `other`. |
| ` double` | ` https://developer.android.com/reference/java/util/OptionalDouble#orElseGet(java.util.function.DoubleSupplier)(https://developer.android.com/reference/java/util/function/DoubleSupplier supplier) ` If a value is present, returns the value, otherwise returns the result produced by the supplying function. |
| ` <X extends https://developer.android.com/reference/java/lang/Throwable> double` | ` https://developer.android.com/reference/java/util/OptionalDouble#orElseThrow(java.util.function.Supplier<?%20extends%20X>)(https://developer.android.com/reference/java/util/function/Supplier<? extends X> exceptionSupplier) ` If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function. |
| ` double` | ` https://developer.android.com/reference/java/util/OptionalDouble#orElseThrow()() ` If a value is present, returns the value, otherwise throws `NoSuchElementException`. |
| ` https://developer.android.com/reference/java/util/stream/DoubleStream` | ` https://developer.android.com/reference/java/util/OptionalDouble#stream()() ` If a value is present, returns a sequential `https://developer.android.com/reference/java/util/stream/DoubleStream` containing only that value, otherwise returns an empty `DoubleStream`. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/util/OptionalDouble#toString()() ` Returns a non-empty string representation of this `OptionalDouble` suitable for debugging. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Public methods

### empty

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static OptionalDouble empty ()
```

Returns an empty `OptionalDouble` instance. No value is present
for this `OptionalDouble`.

<br />

##### API Note:

- Though it may be tempting to do so, avoid testing if an object is empty by comparing with `==` or `!=` against instances returned by `OptionalDouble.empty()`. There is no guarantee that it is a singleton. Instead, use `https://developer.android.com/reference/java/util/OptionalDouble#isEmpty()` or `https://developer.android.com/reference/java/util/OptionalDouble#isPresent()`.

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/OptionalDouble` | an empty `OptionalDouble`. <br /> |

### equals

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean equals (Object obj)
```

Indicates whether some other object is "equal to" this
`OptionalDouble`. The other object is considered equal if:

- it is also an `OptionalDouble` and;
- both instances have no value present or;
- the present values are "equal to" each other via `Double.compare() == 0`.

<br />

<br />

| Parameters ||
|---|---|
| `obj` | `Object`: an object to be tested for equality <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if the other object is "equal to" this object otherwise `false` <br /> |

### getAsDouble

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public double getAsDouble ()
```

If a value is present, returns the value, otherwise throws
`NoSuchElementException`.

<br />

##### API Note:

- The preferred alternative to this method is `https://developer.android.com/reference/java/util/OptionalDouble#orElseThrow()`.

| Returns ||
|---|---|
| `double` | the value described by this `OptionalDouble` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/util/NoSuchElementException` | if no value is present |

### hashCode

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int hashCode ()
```

Returns the hash code of the value, if present, otherwise `0`
(zero) if no value is present.

<br />

| Returns ||
|---|---|
| `int` | hash code value of the present value or `0` if no value is present <br /> |

### ifPresent

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void ifPresent (DoubleConsumer action)
```

If a value is present, performs the given action with the value,
otherwise does nothing.

<br />

| Parameters ||
|---|---|
| `action` | `DoubleConsumer`: the action to be performed, if a value is present <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if value is present and the given action is `null` |

### ifPresentOrElse

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void ifPresentOrElse (DoubleConsumer action, 
                Runnable emptyAction)
```

If a value is present, performs the given action with the value,
otherwise performs the given empty-based action.

<br />

| Parameters ||
|---|---|
| `action` | `DoubleConsumer`: the action to be performed, if a value is present <br /> |
| `emptyAction` | `Runnable`: the empty-based action to be performed, if no value is present <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if a value is present and the given action is `null`, or no value is present and the given empty-based action is `null`. |

### isEmpty

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isEmpty ()
```

If a value is not present, returns `true`, otherwise
`false`.

<br />

| Returns ||
|---|---|
| `boolean` | `true` if a value is not present, otherwise `false` <br /> |

### isPresent

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isPresent ()
```

If a value is present, returns `true`, otherwise `false`.

<br />

| Returns ||
|---|---|
| `boolean` | `true` if a value is present, otherwise `false` <br /> |

### of

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static OptionalDouble of (double value)
```

Returns an `OptionalDouble` describing the given value.

<br />

| Parameters ||
|---|---|
| `value` | `double`: the value to describe <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/OptionalDouble` | an `OptionalDouble` with the value present <br /> |

### orElse

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public double orElse (double other)
```

If a value is present, returns the value, otherwise returns
`other`.

<br />

| Parameters ||
|---|---|
| `other` | `double`: the value to be returned, if no value is present <br /> |

| Returns ||
|---|---|
| `double` | the value, if present, otherwise `other` <br /> |

### orElseGet

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public double orElseGet (DoubleSupplier supplier)
```

If a value is present, returns the value, otherwise returns the result
produced by the supplying function.

<br />

| Parameters ||
|---|---|
| `supplier` | `DoubleSupplier`: the supplying function that produces a value to be returned <br /> |

| Returns ||
|---|---|
| `double` | the value, if present, otherwise the result produced by the supplying function <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if no value is present and the supplying function is `null` |

### orElseThrow

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public double orElseThrow (Supplier<? extends X> exceptionSupplier)
```

If a value is present, returns the value, otherwise throws an exception
produced by the exception supplying function.

<br />

##### API Note:

- A method reference to the exception constructor with an empty argument list can be used as the supplier. For example, `IllegalStateException::new`

| Parameters ||
|---|---|
| `exceptionSupplier` | `Supplier`: the supplying function that produces an exception to be thrown <br /> |

| Returns ||
|---|---|
| `double` | the value, if present <br /> |

| Throws ||
|---|---|
|   | if no value is present |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if no value is present and the exception supplying function is `null` |
| `https://developer.android.com/reference/java/lang/Throwable` |   |

### orElseThrow

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public double orElseThrow ()
```

If a value is present, returns the value, otherwise throws
`NoSuchElementException`.

<br />

| Returns ||
|---|---|
| `double` | the value described by this `OptionalDouble` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/util/NoSuchElementException` | if no value is present |

### stream

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public DoubleStream stream ()
```

If a value is present, returns a sequential `https://developer.android.com/reference/java/util/stream/DoubleStream`
containing only that value, otherwise returns an empty
`DoubleStream`.

<br />

##### API Note:

- This method can be used to transform a `Stream` of optional doubles to a `DoubleStream` of present doubles:

      Stream<OptionalDouble> os = ..
          DoubleStream s = os.flatMapToDouble(OptionalDouble::stream)

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/stream/DoubleStream` | the optional value as a `DoubleStream` <br /> |

### toString

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String toString ()
```

Returns a non-empty string representation of this `OptionalDouble`
suitable for debugging. The exact presentation format is unspecified and
may vary between implementations and versions.

<br />

##### Implementation Requirements:

- If a value is present the result must include its string representation in the result. Empty and present `OptionalDouble`s must be unambiguously differentiable.

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the string representation of this instance <br /> |