Consumer is single argument functional interface like Predicate but it does not return any value. As Consumer is functional interface, so it can be used as assignment target for lambda expressions.
Table of Contents
Consumer definition
Consumer
takes single argument and do not return any result.
Here is the definition of Consumer
interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@FunctionalInterface public interface Consumer<T> { /** * Performs this operation on the given argument. * * @param t the input argument */ void accept(T t); default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } } |
It has a functional method called accept()
and default method andThen()
.
Consumer examples
accept() method example
Let’s use Consumer interface to print String:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; package org.arpit.java2blog; import java.util.function.Consumer; public class Java8ConsumerExample { public static void main(String[] args) { Consumer<String> consumerString=s->System.out.println(s); consumerString.accept("Arpit"); } } |
We have created consumer
object which takes String
object as input and print it.It is simple use of Consumer interface to print String.
When you run above program, you will get below output:
default andThen() method example
As per java docs:
Returns a composed Consumer
that performs, in sequence, this operation followed by the after operation. If performing either operation throws an exception
, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the after operation will not be performed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.util.function.Consumer; public class Java8ConsumerExample { public static void main(String[] args) { Consumer<String> firstC = x -> System.out.println(x.toLowerCase()); Consumer<String> secondC = y -> System.out.println(y.toUpperCase()); Consumer<String> result = firstC.andThen(secondC); result.accept("Arpit"); } } |
Output:
ARPIT
As you can see, we have created two consumers and used andThen()
method to create composite consumer. When we called accept()
method on composite consumer, both the consumers are called in sequence.
Consumer foreach example
Let’s say you have Student
class as below:
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 |
package org.arpit.java2blog; public class Student { private int id; private String name; private String gender; private int age; public Student(int id, String name, String gender, int age) { super(); this.id = id; this.name = name; this.gender = gender; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + "]"; } } |
Now let’s create Consumer object in main class and pass it to forEach method of list:
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 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; public class Java8ConsumerStudentExample { public static void main(String[] args) { List<Student> studentList = createStudentList(); // Creating Consumer for student object which will be used in forEach method of // list Consumer<Student> consumerForStudent = s -> System.out.println(s); studentList.forEach(consumerForStudent); } public static List<Student> createStudentList() { List<Student> studentList = new ArrayList<>(); Student s1 = new Student(1, "Arpit", "M", 19); Student s2 = new Student(2, "John", "M", 17); Student s3 = new Student(3, "Mary", "F", 14); Student s4 = new Student(4, "Martin", "M", 21); Student s5 = new Student(5, "Monica", "F", 16); Student s6 = new Student(6, "Ally", "F", 20); studentList.add(s1); studentList.add(s2); studentList.add(s3); studentList.add(s4); studentList.add(s5); studentList.add(s6); return studentList; } } |
When you run above program, you will get below output:
We have created consumer object which takes student
object as input and print it.Above created consumer object is then passed to foreach()
method of Iterable interface.
1 2 3 4 5 6 7 8 |
default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } |
consumer
as input and calls accept()
method on each object of Iterable.That’s all about Java 8 consumer example.