|
| 1 | +# Introduction |
| 2 | + |
| 3 | +## Optional |
| 4 | + |
| 5 | +The **Optional<T>** type was introduced in Java 8 as a way to indicate that a method _may_ return a value. |
| 6 | + |
| 7 | +In other words, there is a chance the method returns "no value" at all. |
| 8 | + |
| 9 | +## Creating an Optional<T> object |
| 10 | + |
| 11 | +Given an object of type Employee, an Optional<Employee> object can be created using the static [of][optional-of-javadoc] method: |
| 12 | + |
| 13 | +```java |
| 14 | +Employee employee = new Employee(); |
| 15 | +Optional<Employee> optionalEmployee = Optional.of(employee); |
| 16 | +``` |
| 17 | + |
| 18 | +If the employee _may_ be not present, the static [ofNullable][optional-ofNullable-javadoc] method must be used: |
| 19 | + |
| 20 | +```java |
| 21 | +Employee nullableEmployee = new Employee(); |
| 22 | +Optional<Employee> nullableEmployee = Optional.ofNullable(employee); |
| 23 | +``` |
| 24 | + |
| 25 | +`optionalEmployee` and `nullableEmployee` both are wrappers of an `Employee` object. |
| 26 | + |
| 27 | +## Basic methods |
| 28 | + |
| 29 | +If a value is present, the [isPresent][optional-isPresent-javadoc] method returns true and the [get][optional-get-javadoc] method returns the value. |
| 30 | + |
| 31 | +```java |
| 32 | +Employee employee = new Employee("Tim", 45); |
| 33 | +Optional<Employee> optionalEmployee = Optional.ofNullable(employee); |
| 34 | +boolean isThereAnEmployee = optionalEmployee.isPresent(); // true |
| 35 | +Employee employee = optionalEmployee.get(); |
| 36 | +``` |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +In order to throw an exception when the value is not present, the [orElseThrow][optional-orElseThrow-javadoc] method must be used. |
| 41 | + |
| 42 | +```java |
| 43 | +public Optional<Employee> getEmployee(String name) { |
| 44 | + // Assume that getEmployeeByName returns an Optional<Employee> |
| 45 | + return getEmployeeByName(name) |
| 46 | + .orElseThrow(() -> new IllegalArgumentException("Employee not found")); |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +If a default value must be returned, the [orElse][optional-orElse-javadoc] method can be used. |
| 51 | + |
| 52 | +```java |
| 53 | +public Optional<Employee> getEmployee(String name) { |
| 54 | + // Assume that getEmployeeByName returns an Optional<Employee> |
| 55 | + return getEmployeeByName(name) |
| 56 | + .orElse(new Employee("Daniel")); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +Other commonly used method is [ifPresentOrElse][optional-ifPresentOrElse-javadoc], which is used to handle both cases with the same method: the case where the value is present and the case where the value is empty. |
| 61 | + |
| 62 | +```java |
| 63 | +public Optional<Employee> getEmployee(String name) { |
| 64 | + // Assume that getEmployeeByName returns an Optional<Employee> |
| 65 | + return getEmployeeByName(name) |
| 66 | + .ifPresentOrElse( |
| 67 | + employee -> System.out.println(employee.getName()), |
| 68 | + () -> System.out.println("Employee not found") |
| 69 | + ); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Provided all the invoked methods return Optional objects, many methods can be chained without having to worry about null checking: |
| 74 | + |
| 75 | +```java |
| 76 | +public Optional<Integer> getEmployeeAge(String name) { |
| 77 | + Optional<Employee> optionalEmployee = getEmployeeByName(name); |
| 78 | + return getEmployeeByName(name) |
| 79 | + .map(employee -> employee.getAge()) |
| 80 | + .orElse("No employee found"); |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +[optional-of-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#of(java.lang.Object) |
| 85 | +[optional-ofNullable-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#ofNullable(java.lang.Object) |
| 86 | +[optional-get-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#get() |
| 87 | +[optional-isPresent-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#isPresent() |
| 88 | +[optional-orElse-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#orElse(T) |
| 89 | +[optional-orElseThrow-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#orElseThrow() |
| 90 | +[optional-ifPresentOrElse-javadoc]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Optional.html#ifPresentOrElse(java.util.function.Consumer,java.lang.Runnable) |
0 commit comments