Java SE API

Optional.isPresent()

Test whether a value exists, including zero or false. Java SE API; example uses Java 21-compatible syntax.

Formal Syntax & Structure

java syntaxjava
boolean isPresent()

Return Value

boolean

Test whether a value exists, including zero or false.

Working Example

Example Snippetjava
import java.util.*;import java.util.stream.*;public class Main {    public static void main(String[] args) {        Map<String,Integer> stock = Map.of("P1", 0, "P2", 7);        Optional<Integer> found = Optional.ofNullable(stock.get("P1"));        Optional<Integer> missing = Optional.ofNullable(stock.get("P9"));        System.out.println(found.isPresent());        System.out.println(found.orElseThrow());        System.out.println(missing.isEmpty());        System.out.println(Optional.<Integer>empty().isEmpty());        System.out.println(Optional.of(7).isPresent());    }}
Java Reference