Java 8 Optional is not just for replacing a null value

Overview

In Java 8, you can return an Optional instead of return null; as you might do in Java 7.  This may or may not make a big difference depending on whether you tend to forget to check for null or whether you use static code analysis to check to nullalbe references.

However, there is a more compelling case which is to treat Optional like a Stream with 0 or 1 values.

Simple Optional Use Case

In the old days of Java 7 you would write something like

String text = something();
if (text != null) {


With Optional you can instead write

Optional text = something();
if (text.isPresent()) {
    String text2 = text.get();

However, if you are being paranoid you might write.

Optional text = something();
if (text != null && text.isPresent()) {
    String text2 = text.get();

If you have NullPointerException errors often in your project Optional might help, but otherwise it's not looking like it helps much. 

A more complex example

Lets instead consider this example

static String getFirstSecondThird(Nested nested) {
    try {
          return ((Contained2) nested.first.second).get(0).third;
    } catch (NullPointerException | 
             ClassCastException | 
             IndexOutOfBoundsException ignored) {
          return null;
    }
}

This is really ugly.  Instead of catching exceptions, you can build a long list of condition check but it becomes really hard to see what you are trying to do.

Optional allows you to handle all the possible error conditions without an Exception or nested if/else logic.

static Optional getFirstSecondThird(Optional nested) {
    return nested // could be non-present
            .map(x -> x.first) // could be null
            .map(x -> x.second) // could be null
            // could be another type
            .map(x -> x instanceof Contained2 ? (Contained2) x : null)
            .map(x -> x.list) // could be null
            .filter(x -> !x.isEmpty()) // could be empty
            .map(x -> x.get(0)) // could be null
            .map(x -> x.third); // could be null.
}

What we get is a series of mappings and filters which only progress if the value is non-null and present. If any value is null, or a filter is not true, the whole result is "not present".

Conclusion

Using Optional can be a powerful way to navigate a complex data structure in a safe way.  The purpose of lambdas is to reduce boiler plate code, and in the case it avoids all the checks or errors you have.

Additional

For your interest, here is the classes I used in the example above.

static class Nested {
    Contained first;
}

static class Contained {
    IContained2 second;
}

interface IContained2 {
}

static class Contained2 implements IContained2 {
    List list;
}

static class Data {
    String third;
}

Comments

  1. Fluent chain of map and filter transformation is certainly better than catching NPE, IOOBE and whatever exception. However, I can't help thinking there should have been Elvis operator at first. Then, majority use cases would be solvable without using functional solution (I admit, not your example because of cast and get(0)).

    In Optional, I also miss bridge to Collections API in the manner of Guava http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html#asSet%28%29 . On the contrary Guava Optional throws NPE when transforming function returns null, which is annoying and solved better in Java8.

    ReplyDelete
  2. This is plain Java doing the same

    Contained2 a = nested instanceof Contained2 ? (Contained2) nested : null;
    First first = nested!=null ? nested.first : null;
    Second second = first!=null ? first.second : null;
    Elem elem = second!=null && !second.isEmpty ? second.get(0) : null;
    return elem!=null ? elem.third : null;

    The type declarations are a smell, but with lombok `val` can be used. It's not nice, but it's not hacky either and it's clearer and simpler than mapmapmapmap. With Guava and a tiny portion of syntactic sugar you get

    return Iterables.getFirst((nested as? Contained2)?.first?.second, null)?.third;

    Both the fail-safe cast and the null-safe dereferencing exist e.g. in Kotlin. Obviously, with `Optional` being part of Java the harm has been done and we can't hope to get them anytime soon.

    So may I suggest to change the title to something like "Optional is worse than useless"? :D

    ReplyDelete
  3. Thanks for posting the additional code; I'd like to see more tech blog post the whole code samples to help those amongst us who might not immediately 'get' everything that is going on. Unfortunately, there was still some work to getting it to compile, and I had to make some modifications:

    static Optional getFirstSecondThird(Optional nested) {

    should be:

    static Optional getFirstSecondThird(Optional nested) {

    And:

    static class Contained2 implements IContained2 {
    List list;
    }

    could be (either this, or we add a get(int) method):

    static class Contained2 extends ArrayList implements IContained2 {
    List list;
    }

    Maybe I've got them wrong, but this is the only way I could get it to compile.

    Thanks,
    Sok

    ReplyDelete

Post a Comment

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Unusual Java: StackTrace Extends Throwable