isPresent
IsPresent is a method found on Optional types in Java and on similar Optional implementations in other libraries. It returns a boolean that indicates whether the Optional currently contains a value.
In Java's java.util.Optional<T>, isPresent returns true if a non-null value has been wrapped inside the Optional,
- Simple check: Optional<String> maybe = Optional.ofNullable(input); if (maybe.isPresent()) { String value = maybe.get(); }
- Functional style: maybe.ifPresent(v -> doSomething(v));
- Transformations and defaults without explicit checks: use map or flatMap to transform when present, or orElse/orElseGet/orElseThrow
- isPresent is a straightforward predicate, but overusing it with get() can lead to less idiomatic code;
- The Optional type, introduced in Java 8, is designed to model optional values and reduce null-related
- Similar semantics exist in other libraries (for example, Guava’s Optional also provides isPresent), though exact behavior