Close

Java 10 - java.util.Optional Changes

[Last Updated: Mar 31, 2018]

In Java 10, following new method has been added to java.util.Optional class:

public T orElseThrow()

Just like existing Optional#get(), this method returns the value if it is present, otherwise throws NoSuchElementException. The reason for this new addition is that the existing method get() name is misleading because generally a getter method does not throw error. orElseThrow() is the preferred alternative to get() method, whose name is consistent with other or-else methods (ifPresentOrElse(), or(), orElse() and orElseGet()). The old get() method might be deprecated in a future release.

Examples

public class OrElseThrowExample {
  public static void main(String[] args) {
      String result = Stream.of("outlet", "puddle", "eraser")
                        .filter(s -> Character.isDigit(s.charAt(0)))
                        .findAny()
                        .orElseThrow();
      System.out.println(result);
  }
}
java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.orElseThrow(Optional.java:371)
at com.logicbig.example.OrElseThrowExample.main(OrElseThrowExample.java:10)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.logicbig.invoker.MethodCaller.main(MethodCaller.java:31)
public class OrElseThrowExample2 {
  public static void main(String[] args) {
      String result = Stream.of("outlet", "puddle", "eraser")
                            .filter(s -> Character.isAlphabetic(s.charAt(0)))
                            .findAny()
                            .orElseThrow();
      System.out.println(result);
  }
}
outlet

Other Optional classes

The corresponding methods have also been added to OptionalInt, OptionalLong and OptionalDouble as well, as alternatives to existing methods getAsInt(), getAsLong() and getAsDouble() respectively.

Following is OptionalInt example:

public class OrElseThrowIntExample {
  public static void main(String[] args) {
      int result = ThreadLocalRandom.current().ints(5)
                                    .filter(i -> i % 10 == 0)
                                    .findAny()
                                    .orElseThrow();//alternative to getAsInt()
      System.out.println(result);
  }
}
java.util.NoSuchElementException: No value present
at java.base/java.util.OptionalInt.orElseThrow(OptionalInt.java:234)
at com.logicbig.example.OrElseThrowIntExample.main(OrElseThrowIntExample.java:10)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.logicbig.invoker.MethodCaller.main(MethodCaller.java:31)

Example Project

Dependencies and Technologies Used:

  • JDK 9.0.1
Java 10 java.util.Optional New Methods Examples Select All Download
  • java-10-optional-new-methods
    • src
      • com
        • logicbig
          • example
            • OrElseThrowExample.java

    See Also