Skip to content

Commit 70000fd

Browse files
committed
Optional: filter(), map() and flatMap()
1 parent 472bce2 commit 70000fd

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.learn.data;
2+
3+
public class Bike {
4+
5+
private String name;
6+
private String model;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
this.name = name;
14+
}
15+
16+
public String getModel() {
17+
return model;
18+
}
19+
20+
public void setModel(String model) {
21+
this.model = model;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "Bike{" +
27+
"name='" + name + '\'' +
28+
", model='" + model + '\'' +
29+
'}';
30+
}
31+
}

Modern-Java-Examples/src/com/learn/data/Student.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.learn.data;
22

33
import java.util.List;
4+
import java.util.Optional;
45

56
public class Student {
67

@@ -10,6 +11,7 @@ public class Student {
1011
private double gpa;
1112
private List<String> activities;
1213
private int noteBooks;
14+
private Optional<Bike> bike = Optional.empty();
1315

1416
public Student(String name, int gradeLevel, String gender, double gpa, List<String> activities) {
1517
this.name = name;
@@ -84,14 +86,24 @@ public void setNoteBooks(int noteBooks) {
8486
this.noteBooks = noteBooks;
8587
}
8688

89+
public Optional<Bike> getBike() {
90+
return bike;
91+
}
92+
93+
public void setBike(Optional<Bike> bike) {
94+
this.bike = bike;
95+
}
96+
8797
@Override
8898
public String toString() {
8999
return "Student{" +
90100
"name='" + name + '\'' +
91-
", gradeLevel='" + gradeLevel + '\'' +
101+
", gradeLevel=" + gradeLevel +
92102
", gender='" + gender + '\'' +
93103
", gpa=" + gpa +
94104
", activities=" + activities +
105+
", noteBooks=" + noteBooks +
106+
", bike=" + bike +
95107
'}';
96108
}
97109
}

Modern-Java-Examples/src/com/learn/data/StudentDataBase.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.Arrays;
55
import java.util.List;
6+
import java.util.Optional;
67
import java.util.function.Supplier;
78

89
public class StudentDataBase {
@@ -11,7 +12,14 @@ public class StudentDataBase {
1112
/**
1213
* Utility Method to return 1 Student.
1314
*/
14-
public static Supplier<Student> studentSupplier = () -> new Student("Adam", 2, "male", 4.5, Arrays.asList("swimming", "basketball"));
15+
public static Supplier<Student> studentSupplier = () -> {
16+
Bike bike = new Bike();
17+
bike.setName("Honda");
18+
bike.setModel("X1");
19+
Student student = new Student("Adam", 2, "male", 4.5, Arrays.asList("swimming", "basketball"));
20+
student.setBike(Optional.ofNullable(bike));
21+
return student;
22+
};
1523

1624
/**
1725
* Dummy Student Data
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.learn.optional;
2+
3+
import com.learn.data.Bike;
4+
import com.learn.data.Student;
5+
import com.learn.data.StudentDataBase;
6+
7+
import java.util.Optional;
8+
9+
public class OptionalMapFlatMapExample {
10+
11+
public static void main(String[] args) {
12+
13+
optionalFilterExample();
14+
optionalMapExample();
15+
optionalFlatMapExample();
16+
}
17+
18+
/**
19+
* If a value is present, and the value matches the given predicate,
20+
* returns an {@code Optional} describing the value, otherwise returns an
21+
* empty {@code Optional}.
22+
* filter
23+
*/
24+
public static void optionalFilterExample() {
25+
26+
Optional<Student> studentOptional = Optional.ofNullable(StudentDataBase.studentSupplier.get());
27+
// apply filter before we get actual value.
28+
studentOptional
29+
.filter(student -> student.getGpa() >= 3.9)
30+
.ifPresent(System.out::println);
31+
32+
}
33+
34+
/**
35+
* If a value is present, returns an {@code Optional} describing (as if by
36+
* the result of applying the given mapping function to
37+
* the value, otherwise returns an empty {@code Optional}.
38+
*
39+
* If the mapping function returns a {@code null} result then this method
40+
* returns an empty {@code Optional}.
41+
*/
42+
public static void optionalMapExample() {
43+
44+
Optional<Student> studentOptional = Optional.ofNullable(StudentDataBase.studentSupplier.get());
45+
if (studentOptional.isPresent()) {
46+
Optional<String> stringOptional = studentOptional
47+
.filter(student -> student.getGpa() >= 3.9)
48+
.map(Student::getName);
49+
System.out.println(stringOptional);
50+
51+
}
52+
}
53+
54+
/**
55+
* To get Optional Object inside an Optional object, the only way to get it
56+
* is by using flatMap()
57+
*
58+
* In Optional Context, flatMap() is used to get the actual value of the Optional object inside
59+
* another Optional object.
60+
*/
61+
public static void optionalFlatMapExample() {
62+
63+
Optional<Student> studentOptional = Optional.ofNullable(StudentDataBase.studentSupplier.get());
64+
65+
Optional<String> stringOptional = studentOptional
66+
.filter(student -> student.getGpa() >= 3.9) // Optional<Student <Optional<Bike> >
67+
.flatMap(Student::getBike) // Optional<Bike>
68+
.map(Bike::getName);
69+
70+
System.out.println(stringOptional);
71+
}
72+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ This repository contains the basic &amp; advance level examples related to Java
6161
* [ofNullable(), of() and empty()](Modern-Java-Examples/src/com/learn/optional/OptionalOfEmptyNullableExample.java)
6262
* [orElse(), orElseGet() and orElseThrow()](Modern-Java-Examples/src/com/learn/optional/OptionalOrElseExample.java)
6363
* [ifPresent() and isPresent()](Modern-Java-Examples/src/com/learn/optional/OptionalPresentExample.java)
64+
* [filter(), map() and flatMap()](Modern-Java-Examples/src/com/learn/optional/OptionalMapFlatMapExample.java)
6465

6566
<hr />
6667

0 commit comments

Comments
 (0)