Java Stream How to - Create Optional from null








Question

We would like to know how to create Optional from null.

Answer

/*from  w  ww .j a  v a2s  .  c  om*/
import java.util.Optional;

public class Main {

  public static void main(String[] args) {
    String str = null;
    Optional value = Optional.of(str);
  }
}