Java implementation of Scala Option
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Java Option Class

Implementation of Scala Option in Java.


Including in Maven

Available from the Guardian github repository:


And check for the latest version number.

Creating options

import static;
import static;
import static;


Option<Integer> s1 = some(6174);
Option<Integer> n1 = none();
Option<Integer> s2 = option(6174);   // is Some(6174)
Option<Integer> n2 = option(null);   // is None

You may have a headache with ensuring correct typing if you use none() in expressions.

Accessing options

Iterable for-syntax is available:

Option<Integer> s = some(6174);
Option<Integer> n = none();

for (Integer i : s) {
// Do something with i = 6174

for (Integer i : n) {
    // Not executed

As are get and getOrElse: Option s = some(6174); Option n = none();

System.out.println(s.get());            // prints 6174
System.out.println(s.getOrElse(5740));  // prints 6174
System.out.println(n.getOrElse(5740));  // prints 5740

Operating with functions and Options

To map, flatMap, foreach, etc on Options, function interfaces are provided:


Option<Integer> s = some(1);
Option<Integer> n = none();

final Function<Integer,Integer> f = new Function<Integer,Integer>() {
   public Integer apply(Integer x) { return x * 2; }

System.out.println(;        // prints Some(2)
System.out.println(;        // prints None

final Function<Integer,Option<Integer>> f2 = new Function<Integer,Option<Integer>>() {
   public Option<Integer> apply(Integer x) { return some(x * 2); }

System.out.println(s.flatMap(f2));   // prints Some(2)
System.out.println(n.flatMap(f2));   // prints None

final UnitFunction<Integer> f3 = new UnitFunction<Integer>() {
   public void apply(Integer x) { System.out.println(x * 2); }

s.foreach(f3);                      // prints 2
n.foreach(f3);                      // does nothing

Filtering and testing

The methods isDefined, isEmpty, filter, exists are implemented as their Scala counterparts.