-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsumerAndConsumerChainingExample.java
More file actions
81 lines (65 loc) · 1.99 KB
/
ConsumerAndConsumerChainingExample.java
File metadata and controls
81 lines (65 loc) · 1.99 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package predefined_functional_interfaces.consumer;
/*
* Example demonstrating Consumer chaining
*/
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class ConsumerAndConsumerChainingExample {
//Consumer(s) for printing student name, marks and grade
static Consumer<Student> printName = s -> System.out.printf("Name: \"%s\" ", s.getName());
static Consumer<Student> printMarks = s -> System.out.printf("Total Marks: \"%s\" ", s.getMarks());
static Consumer<Student> printGrade = s -> System.out.printf("Grade: \"%s\" \n", s.getGrade());
//consumer chaining: andThen()
static Consumer<Student> printNameMarksGrade = printName.andThen(printMarks).andThen(printGrade);
public static void main(String[] args) {
List<Student> studentList = Arrays.asList(new Student(1, "Anshuman", 98, "A+"),
new Student(2, "Yuvraj", 84, "B"),
new Student(3, "Ram", 78, "C"));
for(Student s: studentList) {
printNameMarksGrade.accept(s);
}
}
}
class Student{
int roll;
String name;
double marks;
String grade;
public Student(int roll, String name, double marks, String grade) {
this.roll = roll;
this.name = name;
this.marks = marks;
this.grade = grade;
}
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMarks() {
return marks;
}
public void setMarks(double marks) {
this.marks = marks;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
/*
* Output:
* Name: "Anshuman" Total Marks: "98.0" Grade: "A+"
* Name: "Yuvraj" Total Marks: "84.0" Grade: "B"
* Name: "Ram" Total Marks: "78.0" Grade: "C"
*/