Consumer Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expect an input but produces no output. In this post, we are going to see several implementations of Consumer Interface by using different examples.
Consumer Interface in Java 8 with Examples...!!! Share on X
Look at Consumer Javadoc description below:
Consumer Interface contains 2 methods:
accept
andThen
Let’s discuss these methods:
accept
This method accepts one input argument and performs operation on the given argument.
Syntax:
1 | void accept(T t); |
Below are the several examples to demonstrate accept() method:
Example 1. with Integer
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.function.Consumer; public class ConsumerInterfaceJava8Example1 { public static void main(String args[]) { System.out.println("Ex. 1 - Java 8 Consumer Interface\n"); Consumer <Integer> consumerObj = a -> System.out.println(a * 5); consumerObj.accept(10); } } |
Example 2. with HashMap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.util.HashMap; import java.util.function.Consumer; public class ConsumerInterfaceJava8Example3 { public static void main(String args[]) { System.out.println("Ex. 2 - Java 8 Consumer Interface\n"); Consumer <HashMap <Integer, String>> consumerListObj = map -> System.out.println(map); HashMap <Integer, String> hashMap = new HashMap <Integer, String> (); hashMap.put(1, "Isha"); hashMap.put(3, "Noni"); consumerListObj.accept(hashMap); } } |
Example 3. with ArrayList using Stream API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; public class ConsumerInterfaceJava8Example2 { public static void main(String args[]) { System.out.println("Ex. 3 - Java 8 Consumer Interface\n"); Consumer <List <Integer>> consumerListObj = list -> list.stream().forEach(listValue -> System.out.print(listValue + "\n")); List <Integer> sampleList = new ArrayList <Integer> (); sampleList.add(3); sampleList.add(44); sampleList.add(91); sampleList.add(75); sampleList.add(8); consumerListObj.accept(sampleList); } } |
Example 4. with custom Class object (like ‘Students’ in this case)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.function.Consumer; public class ConsumerInterfaceJava8Example4 { public static void main(String args[]) { System.out.println("Ex. 4 - Java 8 Consumer Interface\n"); Consumer <Students> consumerListObj = student -> System.out.println("Student details: \n" + "\nName: \n" + student.getName() + "\n\nAge:\n" + student.getAge()); Students student = new Students(1, "Isha"); consumerListObj.accept(student); } } |
Students Class:
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 | public class Students { int age; String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Students() {} public Students(int age, String name) { this.age = age; this.name = name; } } |
andThen
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.
Syntax:
1 2 3 4 | default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } |
Below are the several examples to demonstrate andThen() method:
Example 5. with String
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.function.Consumer; public class ConsumerInterfaceJava8Example5 { public static void main(String args[]) { System.out.println("Ex. 5 - Java 8 Consumer Interface\n"); Consumer <String> consumerObj = a - > System.out.println(a.length()); consumerObj.andThen(consumerObj).accept("Techndeck.com"); } } |
Example 6. with ArrayList using Stream API
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 | import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; public class ConsumerInterfaceJava8Example6 { public static void main(String args[]) { System.out.println("Ex. 6 - Java 8 Consumer Interface\n"); //Add 10 to every integer of the list Consumer <List<Integer>> updatedList = list -> { for (int i = 0; i < list.size(); i++) list.set(i, 10 + list.get(i)); }; //Display a list of integers Consumer <List<Integer>> printList = list -> list.stream().forEach(a -> System.out.print(a + "\n")); List <Integer> list = new ArrayList <Integer> (); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); updatedList.andThen(printList).accept(list); } } |
Java 8 Consumer Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.
Consumer Interface in Java 8 with Examples...!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Generate Prime numbers in Java 8
Other Useful References: