-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionalExample.java
More file actions
26 lines (21 loc) · 824 Bytes
/
OptionalExample.java
File metadata and controls
26 lines (21 loc) · 824 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
package com.learnJava8.optional;
import com.learnJava8.data.Student;
import com.learnJava8.data.StudentDataBase;
import java.util.Optional;
public class OptionalExample {
static Optional<String> getStudentNameOptional() {
Optional<Student> studentOptional = Optional.ofNullable(StudentDataBase.studentSupplier.get());
if (studentOptional.isPresent()) {
return studentOptional.map(Student::getName);
}
return Optional.empty();
}
public static void main(String[] args) {
Optional<String> stringOptional = getStudentNameOptional();
if (stringOptional.isPresent()) {
System.out.println("Length of the student name is : " + stringOptional.get().length());
} else {
System.out.println("Name not found");
}
}
}