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/OptionalInt#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/util/OptionalInt#inhmethods)

# OptionalInt

` public final class OptionalInt `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

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

<br />

*** ** * ** ***

A container object which may or may not contain an `int` 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/OptionalInt#orElse(int)` (returns a default value if no value is present) and `https://developer.android.com/reference/java/util/OptionalInt#ifPresent(java.util.function.IntConsumer)` (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/OptionalInt#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/OptionalInt` | ` https://developer.android.com/reference/java/util/OptionalInt#empty()() ` Returns an empty `OptionalInt` instance. |
| ` boolean` | ` https://developer.android.com/reference/java/util/OptionalInt#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this `OptionalInt`. |
| ` int` | ` https://developer.android.com/reference/java/util/OptionalInt#getAsInt()() ` If a value is present, returns the value, otherwise throws `NoSuchElementException`. |
| ` int` | ` https://developer.android.com/reference/java/util/OptionalInt#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/OptionalInt#ifPresent(java.util.function.IntConsumer)(https://developer.android.com/reference/java/util/function/IntConsumer action) ` If a value is present, performs the given action with the value, otherwise does nothing. |
| ` void` | ` https://developer.android.com/reference/java/util/OptionalInt#ifPresentOrElse(java.util.function.IntConsumer,%20java.lang.Runnable)(https://developer.android.com/reference/java/util/function/IntConsumer 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/OptionalInt#isEmpty()() ` If a value is not present, returns `true`, otherwise `false`. |
| ` boolean` | ` https://developer.android.com/reference/java/util/OptionalInt#isPresent()() ` If a value is present, returns `true`, otherwise `false`. |
| ` static https://developer.android.com/reference/java/util/OptionalInt` | ` https://developer.android.com/reference/java/util/OptionalInt#of(int)(int value) ` Returns an `OptionalInt` describing the given value. |
| ` int` | ` https://developer.android.com/reference/java/util/OptionalInt#orElse(int)(int other) ` If a value is present, returns the value, otherwise returns `other`. |
| ` int` | ` https://developer.android.com/reference/java/util/OptionalInt#orElseGet(java.util.function.IntSupplier)(https://developer.android.com/reference/java/util/function/IntSupplier 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> int` | ` https://developer.android.com/reference/java/util/OptionalInt#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. |
| ` int` | ` https://developer.android.com/reference/java/util/OptionalInt#orElseThrow()() ` If a value is present, returns the value, otherwise throws `NoSuchElementException`. |
| ` https://developer.android.com/reference/java/util/stream/IntStream` | ` https://developer.android.com/reference/java/util/OptionalInt#stream()() ` If a value is present, returns a sequential `https://developer.android.com/reference/java/util/stream/IntStream` containing only that value, otherwise returns an empty `IntStream`. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/util/OptionalInt#toString()() ` Returns a non-empty string representation of this `OptionalInt` 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 OptionalInt empty ()
```

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

<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 `OptionalInt.empty()`. There is no guarantee that it is a singleton. Instead, use `https://developer.android.com/reference/java/util/OptionalInt#isEmpty()` or `https://developer.android.com/reference/java/util/OptionalInt#isPresent()`.

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/OptionalInt` | an empty `OptionalInt` <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 `OptionalInt`. The other object is considered equal if:

- it is also an `OptionalInt` and;
- both instances have no value present or;
- the present values are "equal to" each other via `==`.

<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 /> |

### getAsInt

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

```
public int getAsInt ()
```

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/OptionalInt#orElseThrow()`.

| Returns ||
|---|---|
| `int` | the value described by this `OptionalInt` <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 (IntConsumer action)
```

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

<br />

| Parameters ||
|---|---|
| `action` | `IntConsumer`: 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 (IntConsumer 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` | `IntConsumer`: 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 OptionalInt of (int value)
```

Returns an `OptionalInt` describing the given value.

<br />

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

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

### orElse

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

```
public int orElse (int other)
```

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

<br />

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

| Returns ||
|---|---|
| `int` | 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 int orElseGet (IntSupplier supplier)
```

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

<br />

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

| Returns ||
|---|---|
| `int` | 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 int 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 ||
|---|---|
| `int` | 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 int orElseThrow ()
```

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

<br />

| Returns ||
|---|---|
| `int` | the value described by this `OptionalInt` <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 IntStream stream ()
```

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

<br />

##### API Note:

- This method can be used to transform a `Stream` of optional integers to an `IntStream` of present integers:

      Stream<OptionalInt> os = ..
          IntStream s = os.flatMapToInt(OptionalInt::stream)

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/stream/IntStream` | the optional value as an `IntStream` <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 `OptionalInt` 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 `OptionalInt`s must be unambiguously differentiable.

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