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.
Extends
Section titled “Extends”OptionalBase<T>
Type Parameters
Section titled “Type Parameters”T
Methods
Section titled “Methods”[INSPECT]()
Section titled “[INSPECT]()”[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.
Returns
Section titled “Returns”string
Inherited from
Section titled “Inherited from”OptionalBase.[INSPECT]
equals()
Section titled “equals()”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).
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”boolean
filter()
Section titled “filter()”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.
Parameters
Section titled “Parameters”predicate
Section titled “predicate”(value) => boolean
Returns
Section titled “Returns”Optional<T>
Throws
Section titled “Throws”NullPointerException if predicate is null, even when empty
flatMap()
Section titled “flatMap()”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.
Type Parameters
Section titled “Type Parameters”U
Parameters
Section titled “Parameters”mapper
Section titled “mapper”(value) => Optional<U>
Returns
Section titled “Returns”Optional<U>
Throws
Section titled “Throws”NullPointerException if mapper is null (even when empty) or returns null
get():
T
Defined in: src/stream/Optional.ts:173
Returns the value.
Returns
Section titled “Returns”T
Throws
Section titled “Throws”NoSuchElementException (“No value present”) if empty
hashCode()
Section titled “hashCode()”hashCode():
number
Defined in: src/stream/Optional.ts:229
Returns the value’s hash code, or 0 when empty.
Returns
Section titled “Returns”number
ifPresent()
Section titled “ifPresent()”ifPresent(
action):void
Defined in: src/stream/Optional.ts:80
Calls action with the value if one is present; otherwise does nothing.
Parameters
Section titled “Parameters”action
Section titled “action”(value) => void
Returns
Section titled “Returns”void
Throws
Section titled “Throws”NullPointerException if a value is present and action is null
Inherited from
Section titled “Inherited from”OptionalBase.ifPresent
ifPresentOrElse()
Section titled “ifPresentOrElse()”ifPresentOrElse(
action,emptyAction):void
Defined in: src/stream/Optional.ts:88
Calls action with the value if one is present, otherwise calls emptyAction.
Parameters
Section titled “Parameters”action
Section titled “action”(value) => void
emptyAction
Section titled “emptyAction”() => void
Returns
Section titled “Returns”void
Throws
Section titled “Throws”NullPointerException if the function that would be called is null
Inherited from
Section titled “Inherited from”OptionalBase.ifPresentOrElse
isEmpty()
Section titled “isEmpty()”isEmpty():
boolean
Defined in: src/stream/Optional.ts:72
Returns true if no value is present.
Returns
Section titled “Returns”boolean
Inherited from
Section titled “Inherited from”OptionalBase.isEmpty
isPresent()
Section titled “isPresent()”isPresent():
boolean
Defined in: src/stream/Optional.ts:67
Returns true if a value is present.
Returns
Section titled “Returns”boolean
Inherited from
Section titled “Inherited from”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.
Type Parameters
Section titled “Type Parameters”U
Parameters
Section titled “Parameters”mapper
Section titled “mapper”(value) => U | null | undefined
Returns
Section titled “Returns”Optional<NonNullable<U>>
Throws
Section titled “Throws”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.
Parameters
Section titled “Parameters”supplier
Section titled “supplier”() => Optional<T>
Returns
Section titled “Returns”Optional<T>
Throws
Section titled “Throws”NullPointerException if supplier is null (even when present) or returns null
orElse()
Section titled “orElse()”Call Signature
Section titled “Call Signature”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).
Parameters
Section titled “Parameters”T
Returns
Section titled “Returns”T
Inherited from
Section titled “Inherited from”OptionalBase.orElse
Call Signature
Section titled “Call Signature”orElse(
other):T|null
Defined in: src/stream/Optional.ts:96
Returns the value if present, otherwise other; a null fallback gives T | null.
Parameters
Section titled “Parameters”T | null
Returns
Section titled “Returns”T | null
Inherited from
Section titled “Inherited from”OptionalBase.orElse
orElseGet()
Section titled “orElseGet()”Call Signature
Section titled “Call Signature”orElseGet(
supplier):T
Defined in: src/stream/Optional.ts:105
Returns the value if present, otherwise supplier() (called only when empty).
Parameters
Section titled “Parameters”supplier
Section titled “supplier”() => T
Returns
Section titled “Returns”T
Throws
Section titled “Throws”NullPointerException if empty and supplier is null
Inherited from
Section titled “Inherited from”OptionalBase.orElseGet
Call Signature
Section titled “Call Signature”orElseGet(
supplier):T|null
Defined in: src/stream/Optional.ts:110
Returns the value if present, otherwise supplier(), which may return null.
Parameters
Section titled “Parameters”supplier
Section titled “supplier”() => T | null
Returns
Section titled “Returns”T | null
Throws
Section titled “Throws”NullPointerException if empty and supplier is null
Inherited from
Section titled “Inherited from”OptionalBase.orElseGet
orElseThrow()
Section titled “orElseThrow()”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.
Parameters
Section titled “Parameters”exceptionSupplier?
Section titled “exceptionSupplier?”() => unknown
Returns
Section titled “Returns”T
Throws
Section titled “Throws”NoSuchElementException if empty and no supplier is given
Throws
Section titled “Throws”NullPointerException if empty and the supplier, or the error it returns, is null
Inherited from
Section titled “Inherited from”OptionalBase.orElseThrow
stream()
Section titled “stream()”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.
Returns
Section titled “Returns”Stream<T>
toString()
Section titled “toString()”toString():
string
Defined in: src/stream/Optional.ts:234
Returns Optional[value], or Optional.empty.
Returns
Section titled “Returns”string
empty()
Section titled “empty()”
staticempty<T>():Optional<T>
Defined in: src/stream/Optional.ts:152
Returns the empty optional (a shared instance).
Type Parameters
Section titled “Type Parameters”T
Returns
Section titled “Returns”Optional<T>
staticof<T>(value):Optional<NonNullable<T>>
Defined in: src/stream/Optional.ts:160
Returns an optional holding value.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”T
Returns
Section titled “Returns”Optional<NonNullable<T>>
Throws
Section titled “Throws”NullPointerException if value is null or undefined
ofNullable()
Section titled “ofNullable()”
staticofNullable<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.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”T | null | undefined
Returns
Section titled “Returns”Optional<NonNullable<T>>