Java Optional Cheat Sheet

Java 8 Optional Class

Here are the examples again, with the corresponding outputs:

  1. Creation:
public class OptionalCreationExample {
    public static void main(String[] args) {
        // Empty Optional
        Optional<String> emptyOptional = Optional.empty();
        System.out.println("Empty Optional: " + emptyOptional); // Output: Optional.empty

        // Optional with a non-null value
        Optional<String> nonNullOptional = Optional.of("Hello");
        System.out.println("Non-null Optional: " + nonNullOptional); // Output: Optional[Hello]

        // Optional with a nullable value
        String nullableValue = null;
        Optional<String> nullableOptional = Optional.ofNullable(nullableValue);
        System.out.println("Nullable Optional: " + nullableOptional); // Output: Optional.empty
    }
}
  1. Extraction:
public class OptionalExtractionExample {
    public static void main(String[] args) {
        Optional<String> optional = Optional.of("Hello");

        // get()
        String value = optional.get();
        System.out.println("Value: " + value); // Output: Value: Hello

        // orElse()
        String result = optional.orElse("Default Value");
        System.out.println("Result: " + result); // Output: Result: Hello

        // ifPresent()
        optional.ifPresent(v -> System.out.println("Action performed: " + v)); // Output: Action performed: Hello

        // isPresent()
        boolean isPresent = optional.isPresent();
        System.out.println("Is Present? " + isPresent); // Output: Is Present? true

        // orElseGet()
        String valueOrSupplied = optional.orElseGet(() -> "Supplied Value");
        System.out.println("Value or Supplied: " + valueOrSupplied); // Output: Value or Supplied: Hello

        // orElseThrow()
        String valueOrThrow = optional.orElseThrow(() -> new RuntimeException("Value not present"));
        System.out.println("Value or Throw: " + valueOrThrow); // Output: Value or Throw: Hello
    }
}
  1. Mapping & Filtering:
public class OptionalMappingFilteringExample {
    public static void main(String[] args) {
        Optional<String> optional = Optional.of("Hello");

        // map()
        Optional<Integer> mappedOptional = optional.map(s -> s.length());
        System.out.println("Mapped Optional: " + mappedOptional); // Output: Mapped Optional: Optional[5]

        // flatMap()
        Optional<String> flatMappedOptional = optional.flatMap(s -> Optional.of(s.toUpperCase()));
        System.out.println("Flat-Mapped Optional: " + flatMappedOptional); // Output: Flat-Mapped Optional: Optional[HELLO]

        // filter()
        Optional<String> filteredOptional = optional.filter(s -> s.startsWith("H"));
        System.out.println("Filtered Optional: " + filteredOptional); // Output: Filtered Optional: Optional[Hello]
    }
}

These examples demonstrate the usage of different methods of the Optional class in Java, along with the corresponding outputs.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments