forked from vengateshm/Java-Coding-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTester.java
More file actions
33 lines (26 loc) · 917 Bytes
/
Tester.java
File metadata and controls
33 lines (26 loc) · 917 Bytes
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
package java_8.optional;
import java.util.Optional;
import java.util.Random;
import java.util.function.Supplier;
public class Tester {
public static void main(String[] args) {
String[] str = new String[10];
str[0] = "Hello";
Optional<String> valueAt0 = Optional.of(str[0]);
if (valueAt0.isPresent()) {
System.out.println(valueAt0.get());
}
valueAt0.ifPresent(System.out::println);
Optional<String> valueAt5 = Optional.ofNullable(str[5]);
if (valueAt5.isEmpty()) {
System.out.println("Value not present");
}
System.out.println(valueAt5.orElse("Value not found"));
System.out.println(valueAt5.orElseGet(Tester::getRandomName));
}
private static String getRandomName() {
Random random = new Random();
int value = random.nextInt(5);
return "Value " + value;
}
}