-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionalExample1.java
More file actions
127 lines (105 loc) · 3.51 KB
/
Copy pathOptionalExample1.java
File metadata and controls
127 lines (105 loc) · 3.51 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package optional;
import java.util.List;
import java.util.Optional;
import data.Student;
import data.StudentDB;
public class OptionalExample1 {
public static void main(String args[]) {
printNames();
printNameWithOptional();
printNameswithOptionalOf();
printNameswithOrElse();
printNameswithOrElseGet();
printNameswithOrElseThrow();
printNamesIfPresent();
printNamesFilter();
printflatMap();
}
public static void printflatMap() {
System.out.println("**** flatmap****");
Optional<Student> values = Optional.ofNullable(StudentDB.getAllStudents().get(1));
values.flatMap(Student::getSubject).ifPresent((s)->{
System.out.println(s.getName());
});
}
public static void printNamesFilter() {
System.out.println("*** filter **");
// Optional<Student>
// values=Optional.ofNullable(StudentDB.getAllStudents().get(0));
Optional<Student> values = Optional.ofNullable(null);
String val = values.filter(s -> s.getGpa() >= 4.0).map(Student::getName).orElse("no found");
System.out.println(val);
}
public static void printNamesIfPresent() {
Optional<Student> values = Optional.ofNullable(StudentDB.getAllStudents().get(0));
values.ifPresent((Student s) -> {
System.out.println("printNamesIfPresent");
System.out.println(s.getName());
});
}
public static void printNameswithOrElseThrow() {
Optional<Student> values = Optional.ofNullable(StudentDB.getAllStudents().get(0));
String value = values.map(Student::getName).orElseThrow(RuntimeException::new);
System.out.println(value);
// Run time exception
// values=Optional.ofNullable(null);
// value=values.map(Student::getName).orElseThrow(RuntimeException::new);
// System.out.println(value);
}
public static void printNameswithOrElseGet() {
Optional<Student> values = Optional.ofNullable(StudentDB.getAllStudents().get(0));
String value = values.map(Student::getName).orElseGet(() -> "hello world");
System.out.println(value);
values = Optional.ofNullable(null);
value = values.map(Student::getName).orElseGet(() -> "hello world");
System.out.println(value);
}
public static void printNameswithOrElse() {
Optional<Student> values = Optional.ofNullable(StudentDB.getAllStudents().get(0));
String value = values.map(Student::getName).orElse("No value found");
System.out.println(value);
values = Optional.ofNullable(null);
value = values.map(Student::getName).orElse("No value found");
System.out.println(value);
}
public static void printNameswithOptionalOf() {
// Optional<String> value = Optional.of(null);
Optional<String> value = Optional.of("test");
if (value.isPresent()) {
System.out.println(value.get());
return;
}
System.out.println("Not found");
}
public static void printNameWithOptional() {
Optional<String> value = getNameOptionanl();
if (value.isPresent()) {
System.out.println(value.get());
return;
}
System.out.println("No value found");
}
public static Optional<String> getNameOptionanl() {
Optional<Student> student = Optional.ofNullable(StudentDB.getAllStudents().get(0));
if (student.isPresent()) {
return student.map(Student::getName);
}
return Optional.empty();
}
public static void printNames() {
String name = getName();
if (name != null) {
System.out.println("this user name is : " + name);
return;
}
System.out.println("user not found");
}
public static String getName() {
List<Student> students = StudentDB.getAllStudents();
Student student = students.get(0);
if (students != null) {
return student.getName();
}
return null;
}
}