Skip to content
Buy me a coffee

Optional

Defined in: src/stream/Optional.ts:138

A container that may or may not hold a non-null value; null and undefined both mean “absent”. Returned by stream operations that may have no result (findFirst, min, max, reduce(op), Collectors.minBy). Instances are immutable; the empty optional is a shared instance.

  • OptionalBase<T>

T

[INSPECT](): string

Defined in: src/stream/Optional.ts:128

Returns toString(), so Node’s console.log and util.inspect print the optional the same way.

string

OptionalBase.[INSPECT]


equals(o): boolean

Defined in: src/stream/Optional.ts:221

Returns true if o is an Optional and both are empty or both hold equal values (by equals semantics).

unknown

boolean


filter(predicate): Optional<T>

Defined in: src/stream/Optional.ts:182

Returns this optional if it is empty or predicate(value) holds, otherwise the empty optional.

(value) => boolean

Optional<T>

NullPointerException if predicate is null, even when empty


flatMap<U>(mapper): Optional<U>

Defined in: src/stream/Optional.ts:201

Returns mapper(value) if a value is present, otherwise the empty optional. The mapper must return an optional.

U

(value) => Optional<U>

Optional<U>

NullPointerException if mapper is null (even when empty) or returns null


get(): T

Defined in: src/stream/Optional.ts:173

Returns the value.

T

NoSuchElementException (“No value present”) if empty


hashCode(): number

Defined in: src/stream/Optional.ts:229

Returns the value’s hash code, or 0 when empty.

number


ifPresent(action): void

Defined in: src/stream/Optional.ts:80

Calls action with the value if one is present; otherwise does nothing.

(value) => void

void

NullPointerException if a value is present and action is null

OptionalBase.ifPresent


ifPresentOrElse(action, emptyAction): void

Defined in: src/stream/Optional.ts:88

Calls action with the value if one is present, otherwise calls emptyAction.

(value) => void

() => void

void

NullPointerException if the function that would be called is null

OptionalBase.ifPresentOrElse


isEmpty(): boolean

Defined in: src/stream/Optional.ts:72

Returns true if no value is present.

boolean

OptionalBase.isEmpty


isPresent(): boolean

Defined in: src/stream/Optional.ts:67

Returns true if a value is present.

boolean

OptionalBase.isPresent


map<U>(mapper): Optional<NonNullable<U>>

Defined in: src/stream/Optional.ts:192

Returns Optional.ofNullable(mapper(value)) if a value is present (empty when the mapper returns null/undefined), otherwise the empty optional.

U

(value) => U | null | undefined

Optional<NonNullable<U>>

NullPointerException if mapper is null, even when empty


or(supplier): Optional<T>

Defined in: src/stream/Optional.ts:210

Returns this optional if a value is present, otherwise the optional supplier() returns.

() => Optional<T>

Optional<T>

NullPointerException if supplier is null (even when present) or returns null


orElse(other): T

Defined in: src/stream/Optional.ts:94

Returns the value if present, otherwise other (evaluated eagerly, even when a value is present).

T

T

OptionalBase.orElse

orElse(other): T | null

Defined in: src/stream/Optional.ts:96

Returns the value if present, otherwise other; a null fallback gives T | null.

T | null

T | null

OptionalBase.orElse


orElseGet(supplier): T

Defined in: src/stream/Optional.ts:105

Returns the value if present, otherwise supplier() (called only when empty).

() => T

T

NullPointerException if empty and supplier is null

OptionalBase.orElseGet

orElseGet(supplier): T | null

Defined in: src/stream/Optional.ts:110

Returns the value if present, otherwise supplier(), which may return null.

() => T | null

T | null

NullPointerException if empty and supplier is null

OptionalBase.orElseGet


orElseThrow(exceptionSupplier?): T

Defined in: src/stream/Optional.ts:121

Returns the value if present; otherwise throws NoSuchElementException (“No value present”) without an argument, or the error exceptionSupplier() returns.

() => unknown

T

NoSuchElementException if empty and no supplier is given

NullPointerException if empty and the supplier, or the error it returns, is null

OptionalBase.orElseThrow


stream(): Stream<T>

Defined in: src/stream/Optional.ts:216

Returns a Stream of the value, or an empty stream; flatMap(o => o.stream()) drops empty optionals.

Stream<T>


toString(): string

Defined in: src/stream/Optional.ts:234

Returns Optional[value], or Optional.empty.

string


static empty<T>(): Optional<T>

Defined in: src/stream/Optional.ts:152

Returns the empty optional (a shared instance).

T

Optional<T>


static of<T>(value): Optional<NonNullable<T>>

Defined in: src/stream/Optional.ts:160

Returns an optional holding value.

T

T

Optional<NonNullable<T>>

NullPointerException if value is null or undefined


static ofNullable<T>(value): Optional<NonNullable<T>>

Defined in: src/stream/Optional.ts:165

Returns an optional holding value, or the empty optional when value is null or undefined.

T

T | null | undefined

Optional<NonNullable<T>>