Java8 – Optional’s Overview with Examples

javabullets
1,211 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Java8 – Optional’s Overview with Examples

We can reduce the chance of NullPointerException’s in Java using best practices –

  • Don’t return null’s from methods
  • Don’t pass null’s as arguments

You can also use java.lang.Optional. Java8 Optional formalises the approach used in other languages, and already existed in some Java libraries. Optional is simply an object wrapper.

Examples

My approach is similar style to my post Java8 – Streams Cookbook and have included a class with a number of examples using Optional

Here is our base model, without getters and setters -

class Bike {
    private Optional<Wheels> wheels;
    private String brand;
    
    public Bike(Optional<Wheels> wheels, String brand) {
        this.wheels = wheels;
        this.brand = brand;
    }
}

class Wheels {
    private String brand;
    private int spokes;
    
    public Wheels(String brand, int spokes) {
        this.brand = brand;
        this.spokes = spokes;
    }
}
class NoBikeException extends Exception {
	private static final long serialVersionUID = 1L;
}

Empty Optionals

  • Empty optional using Optional.empty()
  • NoSuchElementException
  • ifPresent, orElse, ifPresent, orElseThrow method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Optional;
public class Main {
// Populate Bike with Optional<Wheels>
private static Bike colnagoBike = new Bike(Optional.of(new Wheels("mavic", 32)), "colnago");
// Dont do this - use a Optional.ofNullable
private static Bike nullBike = new Bike(null, "nowheels");
// Use a Optional.ofNullable
private static Bike ofNullableBike = new Bike(Optional.ofNullable(null), "nowheels");
public static void main(String[] args) {
// Empty Optional - empty container with no object
Optional<Bike> optionalEmptyBike = Optional.empty();
// call get() on empty object throws NoSuchElementException
try {
Bike emptyBike = optionalEmptyBike.get();
} catch (java.util.NoSuchElementException e) {
System.out.println("get() on empty Optional throws java.util.NoSuchElementException " + e.getMessage());
}
// isPresent - check if object is empty - but not much advantage over !=
// null checks
if (!optionalEmptyBike.isPresent()) {
System.out.println("isPresent() - ok - but not much improvement on != null");
}
// Better Alternatives -
// orElse - returns a default object if none set
Bike orElseBike = optionalEmptyBike.orElse(colnagoBike);
System.out.println("orElse - Optional is empty so return colnagoBike " + orElseBike.getBrand());
// ifPresent(Consumer<? extends Bike>) - this prints nothing as Optional
// is empty
optionalEmptyBike.ifPresent(bike -> System.out.println("ifPresent(Consumer) returns " + bike.getBrand()));
// orElseThrow - Throw Exception
try {
Bike orElseThrowBike = optionalEmptyBike.orElseThrow(NoBikeException::new);
} catch (NoBikeException nbe) {
System.out.println("orElseThrow NoBikeException");
}
}
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Null Objects In Optionals

  • of - Populate with null object - Throws Exception
  • ofNullable - allows null
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Optional;
public class Main {
// Populate Bike with Optional<Wheels>
private static Bike colnagoBike = new Bike(Optional.of(new Wheels("mavic", 32)), "colnago");
// Dont do this - use a Optional.ofNullable
private static Bike nullBike = new Bike(null, "nowheels");
// Use a Optional.ofNullable
private static Bike ofNullableBike = new Bike(Optional.ofNullable(null), "nowheels");
public static void main(String[] args) {
// of - Populate with null object - Throws Exception
try {
Optional<Bike> optionalNullBike = Optional.of(null);
} catch (java.lang.NullPointerException nfe) {
System.out.println("Cant call Optional.of(null) - " + nfe.getMessage());
}
// ofNullable - allows null
System.out.println("We can pass a null with ofNullable");
Optional<Bike> optionalOfNullableBike = Optional.ofNullable(null);
System.out.println("optionalOfNullableBike.isPresent() returns " + optionalOfNullableBike.isPresent());
}
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Values In Optionals

  • Filtering and mapping in combination with Lambdas
  • flatMap to prevent Optional<Optional>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Optional;
public class Main {
// Populate Bike with Optional<Wheels>
private static Bike colnagoBike = new Bike(Optional.of(new Wheels("mavic", 32)), "colnago");
// Dont do this - use a Optional.ofNullable
private static Bike nullBike = new Bike(null, "nowheels");
// Use a Optional.ofNullable
private static Bike ofNullableBike = new Bike(Optional.ofNullable(null), "nowheels");
public static void main(String[] args) {
// Populated Optional - now begin populating with Object
Optional<Bike> optionalColnagoBike = Optional.of(colnagoBike);
if (optionalColnagoBike.isPresent()) {
System.out.println("isPresent() - ok - but not much improvement on != null");
}
optionalColnagoBike.ifPresent(
bike -> System.out.println("ifPresent(Consumer) returns " + bike.getBrand()));
// Filtering and mapping in combination with Lambdas
Bike orElseThrowBike = optionalColnagoBike.filter(b -> "colnago".equals(b.getBrand())).get();
//
Optional<String> brand = optionalColnagoBike.map(b -> b.getBrand());
brand.ifPresent(b -> System.out.println("brand " + b));
// flatMap to prevent Optional<Optional<Wheels>>
Optional<Wheels> wheels = optionalColnagoBike.flatMap(b -> b.getWheels());
wheels.ifPresent(w -> System.out.println("flatMap - Wheel Brand " + w.getBrand()));
}
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

If you have liked this post, check out my personal blog which contains similar tutorials at www.javabullets.com

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content