-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPrecidateandConsumerExample.java
More file actions
45 lines (29 loc) · 1.21 KB
/
PrecidateandConsumerExample.java
File metadata and controls
45 lines (29 loc) · 1.21 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
package functionalexamples;
import studentdata.Student;
import studentdata.StudentDataBase;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class PrecidateandConsumerExample {
static Predicate<Student> p1 = (s) -> s.getGradeLevel()>=3;
static Predicate<Student> p2 = (s) -> s.getGpa()>=3.9;
public static void main(String[] args) {
getStudentsFilteredBy(p1.and(p2));
}
public static void getStudentsFilteredBy(Predicate<Student> predicatefilter) {
List<Student> studentList = StudentDataBase.getAllStudents();
BiConsumer<String, List<String>> studentBiConsumer = (name, activities) -> System.out.println(name + " : " + activities);
Consumer<Student> studentConsumer = (student) -> {
if(predicatefilter.test(student)){
studentBiConsumer.accept(student.getName(),student.getActivities());
}
};
studentList.forEach(studentConsumer);
/*
Janet : [swimming, gymnastics, aerobics]
Dave : [swimming, gymnastics, soccer]
James : [swimming, basketball, baseball, football]
*/
}
}