Browse by Topic

Popular

Latest

Latest Java Posts

Diagram showing Optional flatMap flattening a nested Optional into a single Optional value
Java

Using Java Optional flatMap to Avoid Nested Optionals

Learn how Java Optional.flatMap() flattens nested Optional results, chains optional-returning operations, and differs from map().

JavaOptionalflatMapFunctional Programming
Diagram showing a value inside an Optional being transformed by a map function into another Optional.
Java

Java Optional map: Transform Values Safely

Learn how to use Optional.map to transform values safely in Java, including chaining, flatMap for nested optionals, and practical edge cases.

OptionalJavamapflatMap
A Java Optional container being unwrapped with orElseThrow, showing a value being returned or an exception being raised.
Java

Java Optional orElseThrow: Usage and Examples

Learn how to use Optional.orElseThrow() to unwrap values, throw custom exceptions, and avoid the pitfalls of get(), orElse(), and orElseGet().

JavaOptionalException HandlingFunctional Programming
Diagram showing Java Optional orElseGet lazy evaluation versus eager orElse
Java

Java Optional orElseGet: Lazy Default Values Explained

Java Optional.orElseGet supplies a default value lazily. See how it differs from orElse, when to use it, and how to avoid unnecessary work and side effects.

JavaOptionalorElseGetLazy Evaluation
Illustration of Java Optional orElse showing a default value being selected when the Optional is empty.
Java

Java Optional orElse: Usage and Pitfalls

Learn how to use Java Optional orElse correctly, including its eager evaluation, differences from orElseGet, and common pitfalls.

Java OptionalorElseorElseGetNull Handling
Illustration of Java Optional.get() showing a value inside an Optional container and a warning about NoSuchElementException.
Java

Java Optional.get(): Usage, Risks, and Safer Alternatives

Learn how Java Optional.get() works, why it throws NoSuchElementException, and safer alternatives like orElse, orElseGet, orElseThrow, and map.

OptionalJavaNoSuchElementExceptionorElseThrow
A Java Optional box with two branches: one for a present value and one for an empty fallback, illustrating ifPresentOrElse.
Java

Java Optional ifPresentOrElse Explained

Learn how to use Optional.ifPresentOrElse to handle present and absent values in a single functional expression, with syntax, examples, and edge cases.

OptionalJava 9ifPresentOrElsenull handling
Diagram showing Java Optional.ifPresent() applying a consumer to a present value, with a null path leading to no operation.
Java

Java Optional ifPresent: Usage and Pitfalls

Learn how to use Java Optional.ifPresent() correctly, manage side effects, avoid common mistakes, and decide when it is a better choice than traditional null checks.

JavaOptionalifPresentNull Safety
Illustration of Java Optional.isEmpty() method showing an empty box versus a box with a value.
Java

java optional isempty: Using Optional.isEmpty() in Java

java optional isempty: Learn how to use Optional.isEmpty() in Java 11+ to check for absent values, compare it with isPresent(), and avoid common pitfalls.

OptionalJava 11isEmptyJava Streams
Illustration of a Java Optional container with a checkmark for presence and a warning sign about using get() directly, representing safe handling choices.
Java

Java Optional isPresent: Usage and Pitfalls

Understand when Java's Optional.isPresent() is appropriate, why pairing it with get() is an anti-pattern, and how ifPresent(), orElse(), and map() make code safer and more concise.

Java OptionalNull SafetyFunctional ProgrammingException Handling