Optional.get(): retrieving a value from Optional
`Optional.get()` returns the contained value when one is present. Calling it on an empty `Optional` throws `NoSuchElementException`.
CKTIS / Java
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
`Optional.get()` returns the contained value when one is present. Calling it on an empty `Optional` throws `NoSuchElementException`.
Optional.ifPresent: practical reference: Optional.ifPresent(action) invokes the Consumer exactly once with the present value and performs no action for an empty Optional.
Optional.ifPresentOrElse: practical reference: Optional.ifPresentOrElse(action, emptyAction) invokes the Consumer with the present value or runs the Runnable when absent; exactly one branch is selected.
Check whether an `Optional` contains a value without retrieving it. Use `isPresent()` when you need a `boolean` and `ifPresent(...)` when you only want to act on the value if it exists.
Optional.map: practical reference: Optional.map(mapper) applies the Function to a present value and wraps a non-null result, while a null result or empty receiver yields Optional.empty().
Optional.of: practical reference: Optional.of(value) creates an Optional with a necessarily present non-null value and preserves its inferred generic type.
Optional.ofNullable: practical reference: Optional.ofNullable(value) returns Optional.empty() for null and retains a non-null value in the same way as Optional.of.
Optional.orElse: practical reference: Optional.orElse(other) returns the present value, or the supplied other object when the Optional is empty.
Optional.orElseGet: practical reference: Optional.orElseGet(supplier) returns the present value and invokes the supplier only for an empty Optional, returning its result.
Optional.orElseThrow: practical reference: Optional.orElseThrow returns the present value; the no-argument form throws NoSuchElementException when empty, while the supplier form throws the supplied exception.
OutOfMemoryError signals a JVM memory allocation problem. It is a subtype of Error, not Exception, and needs no throws declaration. It does not automatically mean a memory leak or merely a full Java heap.
Compile a regular expression into a `Pattern` object, then reuse that pattern to check more than one input.
`Pattern.matcher` creates a new `Matcher` for the given input. Reuse one compiled pattern across multiple texts while keeping their matching state separate.
Find the calendar period between two dates and read its components. If you need the total number of days, calculate it directly from the dates.
PhantomReference<T> supports notification of changes in an object's reachability without exposing it through get(). It works with ReferenceQueue; the example demonstrates deterministic, manual enqueueing.
Use `Phaser` to synchronize tasks across repeated phases, admit a worker during an active phase, and terminate the barrier when the last party leaves.
Combine two predicates into one reusable condition. The second predicate is evaluated only if the first returns `true`.
`negate()` returns a predicate for the same input type that reverses the original test result without changing the original predicate.
`Predicate.not()` reverses a condition and returns a predicate you can pass to methods such as `Stream.filter`. It has been available since Java 11.
Combine two predicates into one reusable condition. The second predicate is evaluated only when the first returns `false`.
Use `test(T t)` to check a condition against one input. It returns the primitive `boolean` value `true` if the input meets the condition, or `false` if it does not.
PrintStream.print writes text or a value without adding a line separator. The print(String) overload prints null as the text "null".
PrintStream.printf writes data according to a format string, for example to System.out. It controls how numbers and text are presented and allows an explicit locale.
PriorityBlockingQueue is a thread-safe, logically unbounded priority queue. put inserts without waiting for space, while take removes the least available element and waits when the queue is empty.