Java Learner logo
← Back to all exercises

Optional

Optional filtering

Predict the two lines printed when working with Optional.filter + orElseGet.

Optional

Optional filtering

BeginnerAwaiting attempt

Predict the two lines printed when working with Optional.filter + orElseGet.

import java.util.Optional;

public class Main {
    public static void main(String[] args) {
        Optional<String> badge = Optional.of("Pro");
        String label = badge
                .filter("Pro"::equals)
                .map(value -> value + " Member")
                .orElseGet(() -> "Guest");

        badge = Optional.empty();
        System.out.println(label);
        System.out.println(badge.orElse("Guest"));
    }
}