-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathOptionalDemo.java
More file actions
58 lines (46 loc) · 1.87 KB
/
OptionalDemo.java
File metadata and controls
58 lines (46 loc) · 1.87 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
package com.eazybytes.optional;
import java.util.Optional;
import java.util.Random;
public class OptionalDemo {
public static void main(String[] args) {
String message = sayHello();
if(message != null) {
System.out.println(message.toUpperCase());
}
Optional<String> optionalMsg = sayHi();
if(!optionalMsg.isEmpty()) {
System.out.println(optionalMsg.get().toUpperCase());
}
optionalMsg.ifPresent(msg -> System.out.println("ifPresent : "+msg.toUpperCase()));
optionalMsg.ifPresentOrElse(msg -> System.out.println("ifPresentOrElse : "+msg.toUpperCase()),
() -> System.out.println("Value is absent"));
String msg1 = optionalMsg.orElse("Value is absent");
System.out.println("orElse : " + msg1);
String msg2 = optionalMsg.orElseGet(() -> "Value is absent");
System.out.println("orElseGet : " + msg2);
// String msg3 = optionalMsg.orElseThrow();
// String msg3 = optionalMsg.orElseThrow(() -> new IllegalStateException("Value is absent!"));
// System.out.println("orElseThrow : " + msg3);
Optional<String> mapOptionalStr = optionalMsg.map(String::toUpperCase);
System.out.println(mapOptionalStr);
Optional<String> filterOptionalStr = optionalMsg.filter(value -> value.length()>5);
System.out.println(filterOptionalStr);
}
public static String sayHello() {
int num = new Random().nextInt();
System.out.println(num);
if(num % 2 == 0) {
return "Hello World";
}
return null;
}
public static Optional<String> sayHi() {
int num = new Random().nextInt();
System.out.println(num);
String msg = null;
if(num % 2 == 0) {
msg = "Hi World";
}
return Optional.ofNullable(msg);
}
}