Java Stream How to - Create empty Optional








Question

We would like to know how to create empty Optional.

Answer

import java.util.Optional;
//from www  .  j  ava 2 s. c  o m
public class Main {

  public static void main(String[] args) {
    Optional<Integer> value = Optional.empty();

    System.out.println(value.orElse(42));
    System.out.println(value.isPresent());
    System.out.println(value.get());
  }
}