In this article, we will discuss pre-defined Functional Interface in Java 1.8 version for performing operation (processing) based on input argument but it won’t return any value after processing i.e.; Consumer Functional Interface
1. Consumer Functional Interface:
- This Functional Interface has one abstract method called void accept(T t); which
- Step 1 :- accepts 1 input argument of any data-type
- Step 2 :- performs some operation or processes based on the input argument
- Step 3 :- And doesn’t return any result
- Below is interface Consumer containing accept(); method along with other default & static methods
package java.util.function; import java.util.Objects; @FunctionalInterface public interface Consumer<T> { /** * Performs this operation on the given argument. * * @param t the input argument */ void accept(T t); // other default and static methods }
Examples for Consumer Functional Interface:
Example 1 – program to print given String to console
package net.bench.resources.consumer.example; import java.util.function.Consumer; public class PrintUsingConsumerFI { public static void main(String[] args) { // lambda expression to print to console using Consumer FI Consumer<String> c = s -> System.out.println(s); // invoke Consumer FI for below String values c.accept("Welcome to"); c.accept("Java 8 world"); c.accept("for beginners"); c.accept("@ BenchResources.Net"); c.accept("and also learn Java stacks here"); } }
Output:
Welcome to Java 8 world for beginners @ BenchResources.Net and also learn Java stacks here
Example 2 – program to print Student information to console
package net.bench.resources.consumer.example; import java.util.function.Consumer; class Student { // member variables String name; int marks; // public constructor public Student(String name, int marks) { super(); this.name = name; this.marks = marks; } } public class PrintStudentInfoUsingConsumerFI { public static void main(String[] args) { // lambda expression to print to console using Consumer FI Consumer<Student> c = s -> { System.out.print("Name : " + s.name + "\t"); System.out.println("Marks : " + s.marks); }; // create Student object for printing Student Info using Consumer FI Student[] sArray = { new Student("Vijay", 97), new Student("Ajith", 78), new Student("Vikram", 66), new Student("Surya", 54), new Student("Karthi", 43), new Student("Arya", 31) }; // invoke Consumer FI, iterating through all Students for(Student s : sArray) { c.accept(s); } } }
Output:
Name : Vijay Marks : 97 Name : Ajith Marks : 78 Name : Vikram Marks : 66 Name : Surya Marks : 54 Name : Karthi Marks : 43 Name : Arya Marks : 31
2. Consumer Joining :
- When we want to combine 2 or more Consumer into one, then it is called as Consumer Joining
- “andThen” method is used for Consumer chaining
- For example, c1.andThen(c2).andThen(c3);
package net.bench.resources.consumer.example; import java.util.function.Consumer; class Student { // member variables String name; int marks; // public constructor public Student(String name, int marks) { super(); this.name = name; this.marks = marks; } } public class ConsumerChaining { public static void main(String[] args) { // 1. lambda expression using Consumer FI to print console Consumer<Student> c1 = s -> System.out.println(s.name + " enrolled for Java 8 course"); // 2. lambda expression using Consumer FI to print console Consumer<Student> c2 = s -> System.out.println(s.name + " completed course with " + s.marks + " marks."); // 3. lambda expression using Consumer FI to print console Consumer<Student> c3 = s -> System.out.println(s.name + " successfully onboarded into live Java 8 project"); // 4. lambda expression using Consumer FI to print console Consumer<Student> c4 = s -> System.out.println(s.name + " got good appraisal and moved into new tech role"); // final lambda expression combining above 4 Consumer lambda exp Consumer<Student> c = c1.andThen(c2).andThen(c3).andThen(c4); // create Student object Student student = new Student("Sharukh", 97); // invoking combined lambda expression for Consumer chaining c.accept(student); } }
Output:
Sharukh enrolled for Java 8 course Sharukh completed course with 97 marks. Sharukh successfully onboarded into live Java 8 project Sharukh got good appraisal and moved into new tech role
3. Consumer, Function & Predicate Combining :
- We can combine Consumer, Function & Predicate Functional Interface together for our requirements
- For example, if we want to evaluate Students based on some marks and then provide grades to them and finally printing Student information to console, then we combine Consumer, Function and Predicate to achieve this very easily
- In the below example, we are checking each student by iterating through that whether marks is greater than 60 using Predicate and then applying Function to calculate grades for them and finally printing Student information by invoking Consumer
package net.bench.resources.consumer.example; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; class Student { // member variables String name; int marks; // public constructor public Student(String name, int marks) { super(); this.name = name; this.marks = marks; } } public class StudentResultCalculationAndPrinting { public static void main(String[] args) { // 1. lambda expression to check student scored more than 60 using Predicate FI Predicate<Student> p = s -> s.marks >= 60; // 2. lambda expression to find grade of Student supplied Function<Student, String> f = student -> { // alternatively, we can switch statement as well if(student.marks >= 90) return "Merit"; else if(student.marks >= 75) return "Distinction"; else if(student.marks >= 60) return "First class"; else if(student.marks >= 50) return "Second class"; else if(student.marks >= 35) return "Pass class"; else return "Failed"; }; // 3. lambda expression to print Student Info to console using Consumer FI Consumer<Student> c = s -> { System.out.print("Name : " + s.name + "\t"); System.out.print("Marks : " + s.marks + "\t"); // Step 3 - find grade using Function FI System.out.println("Grade : " + f.apply(s)); }; // create Student object for applying Function Student[] sArray = { new Student("Vijay", 97), new Student("Ajith", 78), new Student("Vikram", 66), new Student("Surya", 54), new Student("Karthi", 43), new Student("Arya", 31) }; // iterate through check and find Student grade using Function FI for(Student s : sArray) { // Step 1 : check student marks using Predicate FI if(p.test(s)) { // Step 2 : print to console using Consumer, after finding grade c.accept(s); } } } }
Output:
Name : Vijay Marks : 97 Grade : Merit Name : Ajith Marks : 78 Grade : Distinction Name : Vikram Marks : 66 Grade : First class
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html
- https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html
Happy Coding !!
Happy Learning !!