Sometimes our requirement 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. Consumer Functional Interface contains one abstract method accept.
Just like Predicate Chaining and Function Chaining, Consumer Chaining is also possible. For this Consumer Functional Interface contains default method andThen().
Example
Next Tutorial : Supplier Functional Interface
Previous Tutorial : Function Functional Interface
interface Consumer<T> { public void accept(T t); }Example
package com.ashok.java8.consumer; import java.util.function.Consumer; /** * * @author ashok.mariyala * */ public class ConsumerTest { public static void main(String[] args) { Consumer<String> c = s -> System.out.println(s); c.accept("Ashok Kumar"); } } Output Ashok KumarDisplay Movie Information by using Consumer
package com.ashok.java8.consumer; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; /** * * @author ashok.mariyala * */ public class DisplayMovieNames { public static void main(String[] args) { List<Movie> list = new ArrayList<>(); populate(list); Consumer<Movie> c = m -> { System.out.println("Movie Name : " + m.name); System.out.println("Movie Hero : " + m.hero); System.out.println("Movie Heroine : " + m.heroine); System.out.println(); }; for(Movie m : list) { c.accept(m); } } public static void populate(List<Movie> list) { list.add(new Movie("Bahubali", "Prabhas", "Anushka")); list.add(new Movie("Geetha Govindam", "Vijay Deverakonda", "Rashmika Mandanna")); list.add(new Movie("Goodachari", "Adivi Sesh", "Sobhita Dhulipala")); } } class Movie { String name; String hero; String heroine; public Movie(String name, String hero, String heroine) { this.name = name; this.hero = hero; this.heroine = heroine; } } Output Movie Name : Bahubali Movie Hero : Prabhas Movie Heroine : Anushka Movie Name : Geetha Govindam Movie Hero : Vijay Deverakonda Movie Heroine : Rashmika Mandanna Movie Name : Goodachari Movie Hero : Adivi Sesh Movie Heroine : Sobhita DhulipalaConsumer 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)First Consumer c1 will be applied followed by c2 and c3.
Example
package com.ashok.java8.consumer; import java.util.function.Consumer; /** * * @author ashok.mariyala * */ public class ConsumerChaining { public static void main(String[] args) { Consumer<MovieResult> c1 = m -> System.out.println("Movie : " + m.name + " is ready to release"); Consumer<MovieResult> c2 = m -> System.out.println("Movie : " + m.name + " is just Released and it is : " + m.result); Consumer<MovieResult> c3 = m -> System.out.println("Movie : " + m.name + " information storing in the database"); Consumer<MovieResult> chainedC = c1.andThen(c2).andThen(c3); MovieResult m1 = new MovieResult("Bahubali", "Hit"); chainedC.accept(m1); MovieResult m2 = new MovieResult("Spider", "Flop"); chainedC.accept(m2); } } class MovieResult { String name; String result; public MovieResult(String name, String result) { this.name = name; this.result = result; } } 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
Next Tutorial : Supplier Functional Interface
Previous Tutorial : Function Functional Interface
No comments:
Post a Comment