Consumer Study Material 211
Consumer Study Material 211
Consumer Study Material 211
Features in
Simple Way
Consumer
Study Material
1 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
Consumer (I)
Sometimes our requirment is we have to provide some input value, perform certain operation, but
not required to return anything,then we should go for Consumer.i.e Consumer can be used to
consume object and perform certain operation.
1) interface Consumer<T>
2) {
3) public void accept(T t);
4) }
Output:
Hello
DURGASOFT
2 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
9) {
10) this.name=name;
11) this.hero=hero;
12) this.heroine=heroine;
13) }
14) }
15) class Test
16) {
17) public static void main(String[] args)
18) {
19) ArrayList<Movie> l= new ArrayList<Movie>();
20) populate(l);
21) Consumer<Movie> c= m->{
22) System.out.println("Movie Name:"+m.name);
23) System.out.println("Movie Hero:"+m.hero);
24) System.out.println("Movie Heroine:"+m.heroine);
25) System.out.println();
26) };
27) for(Movie m : l)
28) {
29) c.accept(m);
30) }
31)
32) }
33) public static void populate(ArrayList<Movie> l)
34) {
35) l.add(new Movie("Bahubali","Prabhas","Anushka"));
36) l.add(new Movie("Rayees","Sharukh","Sunny"));
37) l.add(new Movie("Dangal","Ameer","Ritu"));
38) l.add(new Movie("Sultan","Salman","Anushka"));
39) }
40)
41) }
D:\durgaclasses>java Tes
Movie Name:Bahubali
Movie Hero:Prabhas
Movie Heroine:Anushka
Movie Name:Rayees
Movie Hero:Sharukh
Movie Heroine:Sunny
Movie Name:Dangal
Movie Hero:Ameer
Movie Heroine:Ritu
3 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
Movie Name:Sultan
Movie Hero:Salman
Movie Heroine:Anushka
4 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
42) };
43) Consumer<Student> c=s->{
44) System.out.println("Student Name:"+s.name);
45) System.out.println("Student Marks:"+s.marks);
46) System.out.println("Student Grade:"+f.apply(s));
47) System.out.println();
48) };
49) for(Student s : l)
50) {
51) if(p.test(s))
52) {
53) c.accept(s);
54) }
55) }
56) }
57) public static void populate(ArrayList<Student> l)
58) {
59) l.add(new Student("Sunny",100));
60) l.add(new Student("Bunny",65));
61) l.add(new Student("Chinny",55));
62) l.add(new Student("Vinny",45));
63) l.add(new Student("Pinny",25));
64) }
65) }
Output:
Student Name:Sunny
Student Marks:100
Student Grade:A[Dictinction]
Student Name:Bunny
Student Marks:65
Student Grade:B[First Class]
5 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
Consumer Chaining:
Just like Predicate Chaining and Function Chaining, Consumer Chaining is also possible. For this
Consumer Functional Interface contains default method andThen().
c1.andThen(c2).andThen(c3).accept(s)
6 https://www.youtube.com/durgasoftware
Java 8 New
Features in
Simple Way
Output:
Movie:Bahubali is ready to release
Movie:Bahubali is just Released and it is:Hit
Movie:Bahubali information storing in the database
Movie:Spider is ready to release
Movie:Spider is just Released and it is:Flop
Movie:Spider information storing in the database
7 https://www.youtube.com/durgasoftware