DEV Community

ivan.gavlik
ivan.gavlik

Posted on

How to use Optional class (Java) - best practices

Qick info about Optional

Optional isn't meant to be stored as state.

Optional is not meant to be value. It is container for value so that developer can make checking on its value.

Optional is limited mechanism for library method return types where there needed to be a clear way to represent "no result", and using null for such was overwhelmingly likely to cause errors.
For example, you probably should never use it for something that returns an array of results, or a list of results; instead return an empty array or list.
You should almost never use it as a field of something or a method parameter.
Also routinely using it as a return value for getters would definitely be over-use.

Following quote from an Oracle article:

It is important to note that the intention of the Optional class is not to replace every single null reference. Instead, its purpose is to help design more-comprehensible APIs so that by just reading the signature of a method, you can tell whether you can expect an optional value. This forces you to actively unwrap an Optional to deal with the absence of a value.

How to surrive from null pointer exceptions

You don't want to get null value or NullPointerException when using Optional

1. Don't assign null to an optional variable

Optional<Person> person = null;
Enter fullscreen mode Exit fullscreen mode

Solution: use empty()

Optional<Person> person = Optional.empty();
Enter fullscreen mode Exit fullscreen mode

2. Never call get() directly to get the value

Optional<Person> person = PersonService.getPerson();
Person myPerson = persion.get();
Enter fullscreen mode Exit fullscreen mode

Solution: check value with isPresent() before calling get()

Optional<Person> person = PersonService.getPerson();
if (person.isPresent()) {
   Person myPerson = persion.get();
}
Person myPerson = persion.get();
Enter fullscreen mode Exit fullscreen mode

What to return when there is no value present

1. Don't use isPresent()-get() to return default value

if(status.isPresent()) {
   return status.get();
} else {
   return "UNKNOWN";
}
Enter fullscreen mode Exit fullscreen mode

Solution: use orElse()

return status.orElse("UNKNOWN");
Enter fullscreen mode Exit fullscreen mode

2. Don't use orElse() to return computed value

// it is called even if "status" is not empty
return status.orElse(computeStatus());
Enter fullscreen mode Exit fullscreen mode

Solution: use orElseGet()

// computeStatus() is called only if "status" is empty
return status.orElseGet(this::computeStatus);
Enter fullscreen mode Exit fullscreen mode

3. Use orElseThrow() to throw a exception

return status.orElseThrow(Exception::new);
Enter fullscreen mode Exit fullscreen mode

How to consume optional values

1. Use ifPresent() to consume value or isPresentOrElse() to handle empty case

status.ifPresent(System.out::println);
status.isPresentOrElse(
    System.out::println,
    () -> System.out.println("Status not found")
);
Enter fullscreen mode Exit fullscreen mode

2. Use or() to return other Optional

return status.or(() -> Optional.of("PENDING"));
Enter fullscreen mode Exit fullscreen mode

3. orElse()/orElseThrow() goes nicely with streams and lamdbas (don't break a chain)

return products.stream()
    .filter(e -> e.getPrice() > 200)
    .findFirst()
    .map(Product:getName)
    .orElse("Not found");
Enter fullscreen mode Exit fullscreen mode
Optional<Cart> cart = ...;
Product product = ...;
return cart.
    .filter(e -> e.getItems().contains(product))
    .orElseThrow();
Enter fullscreen mode Exit fullscreen mode

Anti-patterns

1. Don't overuse Optional by chaining its methods for purpose of getting value

Status status = ...;
return Optional.ofNullable(status).orElse("Pending");
Enter fullscreen mode Exit fullscreen mode

Solution

Status status = ...;
return status != null ? status : "Pending";
Enter fullscreen mode Exit fullscreen mode

2. Don't use Optional to return empty Collections or Arrays

Solution: Rely on Collections.emptyList()

return items == null ? Collections.emptyList() : items;
Enter fullscreen mode Exit fullscreen mode

3. Don't use Optional as element in Collections or Maps

4. Avoid boxing and unboxing use non-generic Optional

OprionalInt price == OprionalInt.of(50); 
OprionalLong price == OprionalLong.of(50L); 

Enter fullscreen mode Exit fullscreen mode

5. When designing APIs don't declare filed of type Optional

  • it is not value itself

  • doesn't implement Serializable. It is not intended for use as a property of Java Bean

6. Do not use Optional as constructor or method argument.

Solution: make get method return Optional

class Person {
    private final String name;

    Person(String name) {
        this.name = name;
    }

    public Optional<String> getName() {
       return Optional.ofNullable(name);   
    }
}
Enter fullscreen mode Exit fullscreen mode

Best practices

1. There is no need to Unwrap Optionals for asserting or testing equality

Optional<String> actual = ...;
Optional<String> expected = ...;
assertEquals(expected, actual);
Enter fullscreen mode Exit fullscreen mode

2. Reject wrapped values using .filter()

return password
    .filter(p -> p.length() > 5)
    .isPresent();          
Enter fullscreen mode Exit fullscreen mode

3.Use map() or flatMap() to transform value - no need to use isPresent()

// transform name to uper case if null return Optional.empty() 
return lowerName
    .map(String::toUpperCase);          
Enter fullscreen mode Exit fullscreen mode

3.Use stream() to treat the Optional instance as Stream

List<Prodcut> products = productsId.stream()
   .map(this::fetchProductById)
   .flatMap(Optional::stream)
   .collect(toList());

Optional<Product> fetchProductById(String id) {
// implementation
}
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
arocha profile image
Nefario313

Really helpful tips of using Optional in Java.

Collapse
 
95jai profile image
Jai

It would be a good read if you also provide some rationale behind the statements