Java 8 Optional Example

Optional is new class introduced in Java 8 used to model a potentially missing value without using a null reference. Similar to guava's optional class, it provides a way for developers to give null a name, increase readability to programs, and have the ability to return values that may or may not be present. There is also primitive specialized version of Optional: OptionalInt, OptionalDouble and OptionalLong.

Setup

class Framework {

    String name;
    int communityUsers;
}

get

@Test
public void optional_get () {

    Framework framework = new Framework();
    framework.communityUsers = 200000;
    framework.name = "Java";

    Optional<Framework> optionalFramework = Optional.of(framework);

    assertEquals("Java", optionalFramework.get().name);
}

ifPresent

@Test
public void optional_ifPresent () {

    Framework framework = new Framework();
    framework.communityUsers = 200000;
    framework.name = "Java";

    Optional<Framework> optionalFramework = Optional.of(framework);

    optionalFramework.ifPresent(p -> System.out.println(p.name));

    // or 

    optionalFramework.ifPresent(System.out::println);
}

isPresent

@Test
public void optional_ispresent () {

    Optional<Framework> framework = Optional.of(new Framework());

    assertTrue(framework.isPresent());
}

orElse

@Test
public void optional_orElse () {

    Framework framework = new Framework();
    framework.communityUsers = 200000;
    framework.name = "Java";

    Optional<Framework> nullOptional = Optional.ofNullable(null);

    Framework orElseFramework = nullOptional.orElse(framework);

    assertEquals("Java", orElseFramework.name);
}    

orElseGet

@Test
public void optional_orElseGet () {

    Optional<Framework> optionalFramework = Optional.empty();

    Supplier<Framework> defaulFramework = new Supplier<OptionalExample.Framework>() {

        @Override
        public Framework get() {
            Framework framework = new Framework();
            framework.communityUsers = 200000;
            framework.name = "Java";
            return framework;
        }

    };

    Framework framework = optionalFramework.orElseGet(defaulFramework);

    assertEquals("Java", framework.name);
}    

orElseThrow

@Test(expected=IllegalStateException.class)
public void optional_orElseThrow () {

    Optional<Framework> optionalFramework = Optional.empty();

    optionalFramework.orElseThrow(IllegalStateException::new);

}    

empty

@Test
public void optional_empty () {

    Optional<Framework> optionalFramework = Optional.empty();

    assertFalse(optionalFramework.isPresent());
}    

of

@Test
public void optional_of() {

    Framework framework = new Framework();
    framework.communityUsers = 10000;
    framework.name = "Scala";

    Optional<Framework> optionalFramework = Optional.of(framework);

    assertEquals("Scala", optionalFramework.get().name);
}

ofNullable

@Test
public void optional_ofNullable () {

    Optional<Framework> optionalFramework = Optional.ofNullable(null);

    assertFalse(optionalFramework.isPresent());
}