@@ -8,19 +8,37 @@ In other words, there is a chance the method returns "no value" at all.
88
99## Creating an Optional<T > object
1010
11- Given an object of type Employee, an Optional<Employee > object is created as follows :
11+ Given an object of type Employee, an Optional<Employee > object can be created using the static [ of ] [ optional-of-javadoc ] method :
1212
1313``` java
1414Employee employee = new Employee ();
1515Optional<Employee > optionalEmployee = Optional . of(employee);
1616```
1717
18- ` optionalEmployee ` is a wrapper of ` employee ` .
18+ If the employee _ may _ be not present, the static [ ofNullable ] [ optional-ofNullable-javadoc ] method must be used:
1919
20- TBD: explain empty, present and get.
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+ ```
2137
2238## Usage
2339
40+ In order to throw an exception when the value is not present, the [ orElseThrow] [ optional-orElseThrow-javadoc ] method must be used.
41+
2442``` java
2543public Optional<Employee > getEmployee(String name) {
2644 // Assume that getEmployeeByName returns an Optional<Employee>
@@ -29,7 +47,7 @@ public Optional<Employee> getEmployee(String name) {
2947}
3048```
3149
32- If a default value must be returned, the ` orElse ` method can be used.
50+ If a default value must be returned, the [ orElse] [ optional-orElse-javadoc ] method can be used.
3351
3452``` java
3553public Optional<Employee > getEmployee(String name) {
@@ -39,7 +57,7 @@ public Optional<Employee> getEmployee(String name) {
3957}
4058```
4159
42- Other commonly used method is ` ifPresentOrElse ` , which is used to handle the case where the value is present and the case where the value is empty.
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.
4361
4462``` java
4563public Optional<Employee > getEmployee(String name) {
@@ -62,3 +80,11 @@ public Optional<Integer> getEmployeeAge(String name) {
6280 .orElse(" No employee found" );
6381}
6482```
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