Home Java 8 - Optional
Post
Cancel

Java 8 - Optional

Java 8 - Optional

In Java, null is often used to represent the absence of a value. However, using null can often lead to NullPointerExceptions and make code difficult to reason about. To address this issue, Java 8 introduced the Optional class, which provides a way to express that a value may be absent, without using null.

Optional is a container object that may or may not contain a non-null value. It provides methods to check if a value is present, and to retrieve the value if it is present.

To create an Optional object, you can call the of method, passing in the value you want to wrap:

1
2
Optional<String> optional = Optional.of("value");

In this example, optional is an Optional object that contains the string “value”.

If you want to create an Optional object that may or may not contain a value, you can call the empty method:

1
Optional<String> emptyOptional = Optional.empty();

In this example, emptyOptional is an Optional object that does not contain a value.

You can also create an Optional object from a potentially null value using the ofNullable method:

1
2
String nullableValue = null;
Optional<String> nullableOptional = Optional.ofNullable(nullableValue);

In this example, nullableOptional is an Optional object that does not contain a value, because nullableValue is null.

To check if an Optional object contains a value, you can use the isPresent method:

1
2
3
4
5
6
Optional<String> optional = Optional.of("value");
if (optional.isPresent()) {
    String value = optional.get();
    System.out.println(value);
}

In this example, the isPresent method is used to check if optional contains a value, and if it does, the value is retrieved using the get method.

To retrieve the value from an Optional object, you can also use the orElse method, which returns the value if it is present, or a default value if it is not:

1
2
3
4
Optional<String> optional = Optional.empty();
String value = optional.orElse("default value");
System.out.println(value);

In this example, value is set to “default value”, because optional does not contain a value.

You can also provide a supplier function to the orElseGet method, which is only called if the Optional object does not contain a value:

1
2
3
4
Optional<String> optional = Optional.empty();
String value = optional.orElseGet(() -> "default value");
System.out.println(value);

In this example, value is set to “default value”, because optional does not contain a value, and the supplier function is called to provide the default value.

In addition to the methods discussed above, Optional provides a number of other methods for working with optional values, including filter, map, and flatMap. These methods allow you to perform transformations and computations on optional values in a safe and expressive way.

Conclusion

In conclusion, Optional is a powerful and useful class introduced in Java 8 that provides a way to express that a value may be absent, without using null. By using Optional, you can write code that is more expressive, safer, and easier to reason about, and avoid the pitfalls and complexity associated with using null.

This post is licensed under CC BY 4.0 by the author.