-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptionalOrElseExample.java
68 lines (55 loc) · 2.27 KB
/
OptionalOrElseExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.learn.optional;
import com.learn.data.Student;
import com.learn.data.StudentDataBase;
import java.util.Optional;
public class OptionalOrElseExample {
public static void main(String[] args) {
System.out.println("Optional orElse() : " + optionalOrElse());
System.out.println("Optional orElseGet() : " + optionalOrElseGet());
System.out.println("Optional orElseThrow() : " + optionalOrElseThrow());
}
/**
* <p>
* orElse(T) : it accepts the actual value
* If a value is present, returns the value, otherwise returns
* other value passed.
* </p>
* @return
*/
public static String optionalOrElse() {
Optional<Student> studentOptional = Optional.ofNullable(StudentDataBase.studentSupplier.get());
/**
* Optional<Student> studentOptional = Optional.ofNullable(null); // it return Optional.empty()
* it's going try to map the student object and try to get Name. but since it is empty,
* it won't be able to execute map, and execute the orElse and return John.
*/
return studentOptional.map(Student::getName).orElse("John");
}
/**
* <p>
* orElseGet(Supplier) : it's going to accept a supplier.
* If a value is present, returns the value, otherwise returns the result
* produced by the supplying function.
* </p>
* @return
*/
public static String optionalOrElseGet() {
Optional<Student> studentOptional = Optional.ofNullable(StudentDataBase.studentSupplier.get());
return studentOptional.map(Student::getName).orElseGet(() -> "Default");
}
/**
* @apiNote
* There are 2 different versions of orElseThrow().
* <ul>
* <li>orElseThrow() : it throws NoSuchElementException if no value is present </li>
* <li>orElseThrow(Supplier<? extends X> exceptionSupplier) : it accepts exceptionSupplier the
* supplying function that produces an exception to be thrown</li>
* </ul>
*
*/
public static String optionalOrElseThrow() {
Optional<Student> studentOptional = Optional.ofNullable(null);
return studentOptional.map(Student::getName)
.orElseThrow(() -> new RuntimeException("No Data Found"));
}
}