Junior — Middle
How can you determine if an Optional object contains a non-null value?
sobes.tech AI
Answer from AI
To determine whether an Optional object contains a non-null (non-empty) value, the isPresent() method is usually used.
Example in Java:
Optional<String> optional = Optional.ofNullable(getValue());
if (optional.isPresent()) {
// Value is present
String value = optional.get();
} else {
// Value is absent
}
If isPresent() returns true, it means there is a value inside the Optional; otherwise, the object is empty.