# OptionalLong

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

# OptionalLong

`
public

final

class
OptionalLong
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

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

<br />

*** ** * ** ***

A container object which may or may not contain a`long`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()](https://developer.android.com/reference/java/util/OptionalLong#orElse(long))(returns a default value if no value is present) and[ifPresent()](https://developer.android.com/reference/java/util/OptionalLong#ifPresent(java.util.function.LongConsumer))(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/OptionalLong#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 `[OptionalLong](https://developer.android.com/reference/java/util/OptionalLong)       | ` `[empty](https://developer.android.com/reference/java/util/OptionalLong#empty())`() ` Returns an empty`OptionalLong`instance.                                                                                                                                                                                                                                                                                                                                          |
| ` boolean`                                                                                     | ` `[equals](https://developer.android.com/reference/java/util/OptionalLong#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this`OptionalLong`.                                                                                                                                                                                                                  |
| ` long`                                                                                        | ` `[getAsLong](https://developer.android.com/reference/java/util/OptionalLong#getAsLong())`() ` If a value is present, returns the value, otherwise throws`NoSuchElementException`.                                                                                                                                                                                                                                                                                      |
| ` int`                                                                                         | ` `[hashCode](https://developer.android.com/reference/java/util/OptionalLong#hashCode())`() ` Returns the hash code of the value, if present, otherwise`0`(zero) if no value is present.                                                                                                                                                                                                                                                                                 |
| ` void`                                                                                        | ` `[ifPresent](https://developer.android.com/reference/java/util/OptionalLong#ifPresent(java.util.function.LongConsumer))`(`[LongConsumer](https://developer.android.com/reference/java/util/function/LongConsumer)` action) ` If a value is present, performs the given action with the value, otherwise does nothing.                                                                                                                                                  |
| ` void`                                                                                        | ` `[ifPresentOrElse](https://developer.android.com/reference/java/util/OptionalLong#ifPresentOrElse(java.util.function.LongConsumer,%20java.lang.Runnable))`(`[LongConsumer](https://developer.android.com/reference/java/util/function/LongConsumer)` action, `[Runnable](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`                                                                                     | ` `[isEmpty](https://developer.android.com/reference/java/util/OptionalLong#isEmpty())`() ` If a value is not present, returns`true`, otherwise`false`.                                                                                                                                                                                                                                                                                                                  |
| ` boolean`                                                                                     | ` `[isPresent](https://developer.android.com/reference/java/util/OptionalLong#isPresent())`() ` If a value is present, returns`true`, otherwise`false`.                                                                                                                                                                                                                                                                                                                  |
| ` static `[OptionalLong](https://developer.android.com/reference/java/util/OptionalLong)       | ` `[of](https://developer.android.com/reference/java/util/OptionalLong#of(long))`(long value) ` Returns an`OptionalLong`describing the given value.                                                                                                                                                                                                                                                                                                                      |
| ` long`                                                                                        | ` `[orElse](https://developer.android.com/reference/java/util/OptionalLong#orElse(long))`(long other) ` If a value is present, returns the value, otherwise returns`other`.                                                                                                                                                                                                                                                                                              |
| ` long`                                                                                        | ` `[orElseGet](https://developer.android.com/reference/java/util/OptionalLong#orElseGet(java.util.function.LongSupplier))`(`[LongSupplier](https://developer.android.com/reference/java/util/function/LongSupplier)` supplier) ` If a value is present, returns the value, otherwise returns the result produced by the supplying function.                                                                                                                              |
| ` <X extends `[Throwable](https://developer.android.com/reference/java/lang/Throwable)`> long` | ` `[orElseThrow](https://developer.android.com/reference/java/util/OptionalLong#orElseThrow(java.util.function.Supplier<?%20extends%20X>))`(`[Supplier](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.                                                                                    |
| ` long`                                                                                        | ` `[orElseThrow](https://developer.android.com/reference/java/util/OptionalLong#orElseThrow())`() ` If a value is present, returns the value, otherwise throws`NoSuchElementException`.                                                                                                                                                                                                                                                                                  |
| ` `[LongStream](https://developer.android.com/reference/java/util/stream/LongStream)           | ` `[stream](https://developer.android.com/reference/java/util/OptionalLong#stream())`() ` If a value is present, returns a sequential[LongStream](https://developer.android.com/reference/java/util/stream/LongStream)containing only that value, otherwise returns an empty`LongStream`.                                                                                                                                                                                |
| ` `[String](https://developer.android.com/reference/java/lang/String)                          | ` `[toString](https://developer.android.com/reference/java/util/OptionalLong#toString())`() ` Returns a non-empty string representation of this`OptionalLong`suitable for debugging.                                                                                                                                                                                                                                                                                     |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](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 `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](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`                                                                  | ` `[wait](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`                                                                  | ` `[wait](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 OptionalLong empty ()
```

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

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

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

- it is also an`OptionalLong`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 /> |

### getAsLong

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

```
public long getAsLong ()
```

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

<br />

##### API Note:

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

|                         Returns                          ||
|--------|--------------------------------------------------|
| `long` | the value described by this`OptionalLong` <br /> |

|                                                           Throws                                                           ||
|----------------------------------------------------------------------------------------------------|------------------------|
| [NoSuchElementException](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 (LongConsumer action)
```

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

<br />

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

|                                                                      Throws                                                                       ||
|------------------------------------------------------------------------------------------------|---------------------------------------------------|
| [NullPointerException](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 (LongConsumer 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`      | `LongConsumer`: 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                                                                                                         ||
|------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|
| [NullPointerException](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 OptionalLong of (long value)
```

Returns an`OptionalLong`describing the given value.

<br />

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

|                                                            Returns                                                            ||
|--------------------------------------------------------------------------------|-----------------------------------------------|
| [OptionalLong](https://developer.android.com/reference/java/util/OptionalLong) | an`OptionalLong`with the value present <br /> |

### orElse

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

```
public long orElse (long other)
```

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

<br />

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

|                        Returns                         ||
|--------|------------------------------------------------|
| `long` | 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 long orElseGet (LongSupplier supplier)
```

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

<br />

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

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

|                                                                           Throws                                                                           ||
|------------------------------------------------------------------------------------------------|------------------------------------------------------------|
| [NullPointerException](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 long 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                ||
|--------|------------------------------|
| `long` | the value, if present <br /> |

|                                                                                Throws                                                                                ||
|------------------------------------------------------------------------------------------------|----------------------------------------------------------------------|
|                                                                                                | if no value is present                                               |
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if no value is present and the exception supplying function is`null` |
| [Throwable](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 long orElseThrow ()
```

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

<br />

|                         Returns                          ||
|--------|--------------------------------------------------|
| `long` | the value described by this`OptionalLong` <br /> |

|                                                           Throws                                                           ||
|----------------------------------------------------------------------------------------------------|------------------------|
| [NoSuchElementException](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 LongStream stream ()
```

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

<br />

##### API Note:

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

      Stream<OptionalLong> os = ..
           LongStream s = os.flatMapToLong(OptionalLong::stream)
       
|                                                            Returns                                                             ||
|-----------------------------------------------------------------------------------|---------------------------------------------|
| [LongStream](https://developer.android.com/reference/java/util/stream/LongStream) | the optional value as an`LongStream` <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`OptionalLong`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`OptionalLong`s must be unambiguously differentiable.

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