Added in API level 24

OptionalInt

class OptionalInt
kotlin.Any
   ↳ java.util.OptionalInt

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

Summary

Public methods
static OptionalInt!

Returns an empty OptionalInt instance.

Boolean
equals(other: Any?)

Indicates whether some other object is "equal to" this OptionalInt.

Int

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

Int

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

Unit

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

Unit
ifPresentOrElse(action: IntConsumer!, emptyAction: Runnable!)

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

Boolean

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

Boolean

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

static OptionalInt!
of(value: Int)

Returns an OptionalInt describing the given value.

Int
orElse(other: Int)

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

Int
orElseGet(supplier: IntSupplier!)

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

Int

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

Int
orElseThrow(exceptionSupplier: Supplier<out X>!)

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

IntStream!

If a value is present, returns a sequential IntStream containing only that value, otherwise returns an empty IntStream.

String

Returns a non-empty string representation of this OptionalInt suitable for debugging.

Public methods

empty

Added in API level 24
static fun empty(): OptionalInt!

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

Return
OptionalInt! an empty OptionalInt

equals

Added in API level 24
fun equals(other: Any?): Boolean

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 ==.

Parameters
obj an object to be tested for equality
Return
Boolean true if the other object is "equal to" this object otherwise false

getAsInt

Added in API level 24
fun getAsInt(): Int

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

Return
Int the value described by this OptionalInt
Exceptions
java.util.NoSuchElementException if no value is present

hashCode

Added in API level 24
fun hashCode(): Int

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

Return
Int hash code value of the present value or 0 if no value is present

ifPresent

Added in API level 24
fun ifPresent(action: IntConsumer!): Unit

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

Parameters
action IntConsumer!: the action to be performed, if a value is present
Exceptions
java.lang.NullPointerException if value is present and the given action is null

ifPresentOrElse

Added in API level 33
fun ifPresentOrElse(
    action: IntConsumer!,
    emptyAction: Runnable!
): Unit

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

Parameters
action IntConsumer!: the action to be performed, if a value is present
emptyAction Runnable!: the empty-based action to be performed, if no value is present
Exceptions
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
fun isEmpty(): Boolean

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

Return
Boolean true if a value is not present, otherwise false

isPresent

Added in API level 24
fun isPresent(): Boolean

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

Return
Boolean true if a value is present, otherwise false

of

Added in API level 24
static fun of(value: Int): OptionalInt!

Returns an OptionalInt describing the given value.

Parameters
value Int: the value to describe
Return
OptionalInt! an OptionalInt with the value present

orElse

Added in API level 24
fun orElse(other: Int): Int

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

Parameters
other Int: the value to be returned, if no value is present
Return
Int the value, if present, otherwise other

orElseGet

Added in API level 24
fun orElseGet(supplier: IntSupplier!): Int

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

Parameters
supplier IntSupplier!: the supplying function that produces a value to be returned
Return
Int the value, if present, otherwise the result produced by the supplying function
Exceptions
java.lang.NullPointerException if no value is present and the supplying function is null

orElseThrow

Added in API level 33
fun orElseThrow(): Int

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

Return
Int the value described by this OptionalInt
Exceptions
java.util.NoSuchElementException if no value is present

orElseThrow

Added in API level 24
fun <X : Throwable!> orElseThrow(exceptionSupplier: Supplier<out X>!): Int

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

Parameters
<X> Type of the exception to be thrown
exceptionSupplier Supplier<out X>!: the supplying function that produces an exception to be thrown
Return
Int the value, if present
Exceptions
X if no value is present
java.lang.NullPointerException if no value is present and the exception supplying function is null

stream

Added in API level 33
fun stream(): IntStream!

If a value is present, returns a sequential IntStream containing only that value, otherwise returns an empty IntStream.

Return
IntStream! the optional value as an IntStream

toString

Added in API level 24
fun toString(): String

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.

Return
String the string representation of this instance