Java Stream How to - Create Optional from string








Question

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

Answer

import java.util.Optional;
/*from   w ww  .j  av a 2s. c  o m*/
public class Main {

  public static void main(String[] args) {
    Optional<String> value = Optional.of("some value");
    System.out.println(value.isPresent());
    System.out.println(value.get());
  }
}

The code above generates the following result.