-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOptionalTest.java
More file actions
33 lines (29 loc) · 1.75 KB
/
Copy pathOptionalTest.java
File metadata and controls
33 lines (29 loc) · 1.75 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
package inverview.optional;
import java.util.Optional;
public class OptionalTest {
public static void main(String[] args) {
String[] str = new String[10];
str[5] = "JAVA OPTIONAL CLASS EXAMPLE"; // Setting value for 5th index
// It returns an empty instance of Optional class
Optional<String> empty = Optional.empty();
System.out.println("empty: " + empty);
// System.out.println(empty.get());
// It returns a non-empty Optional
Optional<String> value = Optional.of(str[5]);
// If value is present, it returns an Optional otherwise returns an empty Optional
System.out.println("Filtered value: "+value.filter((s)->s.equals("Abc")));
System.out.println("Filtered value: "+value.filter((s)->s.equals("JAVA OPTIONAL CLASS EXAMPLE")));
// It returns value of an Optional. if value is not present, it throws an NoSuchElementException
System.out.println("Getting value: "+value.get());
// It returns hashCode of the value
System.out.println("Getting hashCode: "+value.hashCode());
// It returns true if value is present, otherwise false
System.out.println("Is value present: "+value.isPresent());
// It returns non-empty Optional if value is present, otherwise returns an empty Optional
System.out.println("Nullable Optional: "+Optional.ofNullable(str[5]));
// It returns value if available, otherwise returns specified value,
System.out.println("orElse: "+value.orElse("Value is not present"));
System.out.println("orElse: "+empty.orElse("Value is not present"));
value.ifPresent(System.out::println); // printing value by using method reference
}
}