java streams: working with optional

introduction


if you want to work with data, you'll often run into situations where you expect a value to be present, but it's possible that the value might not exist. these days, many developers try to avoid null values altogether and use optional objects instead. an optional object is an object that may or may not contain a non-null value. if a method returns an optional object, it means that the method can return either the desired result wrapped in an optional or an empty optional. in this article we'll explore how to work with them in java 8 streams.


when working with java, you will inevitably encounter situations where you want to represent the possibility of a value being absent. this is where an optional comes in handy. it can be a bit confusing to understand how it works, especially if you’ve never used one before. let’s start with some definitions:

  • an optional is either present or not present. it doesn’t exist in between.

  • an empty collection means that there are no elements in it at all; this is different from an empty array (which still has every slot filled with null), and also different from an array containing only one element (which would have its length equal to 1).

creating an optional object


to create an empty optional, call the static function empty() on optional class:

final var optional = Optional.empty();

to create an empty optional from a non-null value:

final var optional = Optional.of("hello world");

to create an empty optional from null:

final var optional = Optional.ofNullable(null);


verify an optional object


before you access a value of an optional, it is highly recommended to check its presents or absents. if you access the object without checking it first, an exception will be thrown. you can check if an optional is empty with the following methods:

.ifPresent(value -> .. );

this is the most general method to test for the presence of a value. It accepts a single block that will be executed only if there is a value in the optional:

.isPresent();

this method returns true when an optional contains a non-null value:

.isEmpty();

this method returns true when an optional does not contain any value at all (either because it was initialized with null, or because it was already checked and found to be empty).


extract value of an optional object


three useful operations are to extract a value from an optional object

.orElse("default");

this method allows to extract the optional value, but if no value is present a defined default value will be returned.

.orElseGet(() -> "default");

this method also allows to extract the optional value, also if no value is present a defined default value will be returned. the difference is that here a supplier (lambda) is provided. the advantage here is that the code is only executed if no value is present. using the former method without lambda, the default value is always processed even if the optional value is present.

.orElseThrow();

this method also allows you to extract the optional value, but if no value is present an exception is thrown. with this approach you always demand a value to be present, since there cannot be a default value anymore.


conclusion


i hope that this article has given you a good idea of how to work with optional in java streams. we have covered the basics from how it is used to why it's useful. also, we have shown some examples to help you get started with using optional in your projects as well.

6 views2 comments

Recent Posts

See All