The Consumer Functional interface takes a single input and does not return anything. The consumer interface is located in java.util.function package. It has a Single Abstract Method (SAM) accept(), which accepts the generic object type T and doesn’t return any result.
Java Consumer Example
Whenever we create a Lambda Expression, which takes a single input and doesn’t return any value, then the Consumer can be used as a target for the lambda expression.
Methods in Consumer Interface
- accept(T t) – This method takes a single generic argument T and doesn’t return any value
- default Consumer<T> andThen(Consumer<? super T> after) – This is a default method, returns a composed consumer. The input consumer will be executed, and on the result, the second consumer will be executed.
1. Java Consumer accept(T t) method example
The accept() method of the Consumer interface can take any object type as an argument and does not return anything
Let’s build a Consumer that prints element which is passed to it.
package com.javainterviewpoint; import java.util.function.Consumer; public class PrintConsumer { public static void main(String[] args) { Consumer printer = str -> System.out.println(str); printer.accept("Welcome"); printer.accept("JavaInterviewPoint"); } }
Output:
Welcome JavaInterviewPoint
In the above code, we have created a Consumer that prints the string passed to it.
Consumer<String> printer = str -> System.out.println(str);
We can invoke the printer consumer by passing a string argument to the accept() method.
The Consumer interface can also be used in the Streams; the forEach() method of the stream takes the Consumer as its argument.
Let’s re-use the PrinterConsumer in a Stream to print the elements of the ArrayList.
package com.javainterviewpoint; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; public class PrintConsumer { public static void main(String[] args) { Consumer printer = str -> System.out.println(str); List countryList = Arrays.asList("India", "Australia", "France", "Canada"); countryList.stream().forEach(printer); } }
Output:
India Australia France Canada
The Consumer can be used on custom objects as well. Let’s create a consumer that capitalizes the student names.
Student.java
package com.javainterviewpoint; public class Student { private int id; private int mark; private String name; public Student() { super(); } public Student(int id, int mark, String name) { super(); this.id = id; this.mark = mark; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getMark() { return mark; } public void setMark(int mark) { this.mark = mark; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [id=" + id + ", mark=" + mark + ", name=" + name + "]"; } }
CapitalizeConsumer.java
package com.javainterviewpoint; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; public class CapitalizeConsumer { public static void main(String[] args) { List<String> studentList = new ArrayList<String>(); studentList.add(new Student(1, 45, "Alice")); studentList.add(new Student(2, 65, "Bob")); studentList.add(new Student(3, 80, "Clair")); studentList.add(new Student(4, 20, "Dom")); Consumer<String> capsConsumer = name -> System.out .println(name.toUpperCase()); studentList.stream().map(student -> student.getName()) .forEach(capsConsumer); } }
In the above code, we have created a consumer that capitalizes and prints the names passed to it.
Output:
ALICE BOB CLAIR DOM
2. Consumer interface andThen() method example
The andThen() method of the Consumer interface, the input function will be executed first, and on the result, the second function (andThen) will be executed.
The first consumer will add 20 marks for each student, and the second consumer prints the student object.
package com.javainterviewpoint; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; public class StudentConsumer { public static void main(String[] args) { List<Student> studentList = new ArrayList<Student>(); studentList.add(new Student(1, 25, "Adam")); studentList.add(new Student(2, 35, "Bob")); studentList.add(new Student(3, 45, "Danny")); studentList.add(new Student(4, 55, "Will")); Consumer<List<Student>> addMarksConsumer = list -> { for (int i = 0; i < list.size(); i++) { list.get(i).setMark(list.get(i).getMark() + 20); } }; Consumer<List<Student>> printConsumer = list -> list .forEach(System.out::println); addMarksConsumer.andThen(printConsumer).accept(studentList); } }
In the above, we have the first consumer addMarksConsumer takes the Student marks and adds 20 to it, and the second consumer printConsumer prints the student object.
Since we have used addMarksConsumer.andThen(printConsumer), the addMarksConsumer will be executed first, and on top of it, the printConsumer gets executed
Output:
Student [id=1, mark=45, name=Adam] Student [id=2, mark=55, name=Bob] Student [id=3, mark=65, name=Danny] Student [id=4, mark=75, name=Will]
Happy Learning !!
Leave a Reply