Monday, April 3, 2023

Java 8 - Consumer Functional Interface Example

Hello guys, if you are wondering what is a Consumer functional interface in Java and when and how to use it then you have come come to the right place.  In the last few article, I have explained what is Functional interface and showed example of Predicate Interface and in this article, I will show you how to use Consumer functional Interface in Java.  A Java Consumer is a predefined function interface that has been introduced by java 8. This type of interface accepts only one argument in order to generate the result

We can use Consumer interface once an object is ready to be used, so we send the input and it executes some operation on the object without returning any kind of value.

 

Java Consumer interface has two methods: accept() , andThen()

a-    accept(): it receives exactly one value and performs some operation based on that argument.

 

Example1:

 public static void main(String[] args) {

    Consumer<String> word = c -> System.
out.println(c);
   
word.accept("Hello World");
   
}

 

The output:

Hello World

 

Example2:

 

public static void main(String[] args) {

    Consumer<List<String>>
            wordsList = list -> list.stream().forEach(c -> System.
out.println(c + " "));

   
List<String> list = new ArrayList<String>();
   
list.add("dog");
   
list.add("cat");
   
list.add("turtle");
   
wordsList.accept(list);
}

 

 

The Output:

 

 dog
 cat
 turtle

 

 

b-   andThen():

It is used when we want Consumer does operation after operation. So we can execute multiple operations sequentially. The argument that is in andThen() method should be Consumer type. 

If one of the operation throws exception then it is forwarded to the composed operation's caller. If the after operation is null, the function andThen() will throw a NullPointerException.

Example:

 

public static void main(String[] args) {

    Consumer<List<String>> modify = list ->
    {
        Collections.sort(list)
;
   
};

   
Consumer<List<String>>
            wordsList = list -> list.stream().forEach(c -> System.
out.println(c + " "));

   
List<String> list = new ArrayList<String>();
   
list.add("orange");
   
list.add("apple");
   
list.add("banana");
   
modify.andThen(wordsList).accept(list);
}

 

The Output:

 apple
 banana
 orange

 

And, here is a nice diagram highlighting important functional interface from Java API:

Java Consumer Functional Interface Example


That's all about what is Consumer Functional interface and how to use it in Java. Just remember that it consumes whatever object you pass to it and it doesn't return anything back. One of the best example of Consumer interface is forEach() method which takes can be used to print an object or do something with the object without returning it. 


 Other Java 8 tutorials and Resources for further learning

  • Top 5 Courses to Learn Java 8 Programming (courses)
  • 8 ways to sort List in Java? (List sorting example)
  • 5 Books to Learn Java 8 from Scratch (books)
  • How to use Stream.distinct() method in Java? (distinct example)
  • How to join String in Java 8 (example)
  • Java Stream + map + flatMap + collect example (map example)
  • How to use filter() method in Java 8 (tutorial)
  • Java 8 map + filter + stream example (tutorial)
  • How to use Stream class in Java 8 (tutorial)
  • How to use forEach() method in Java 8 (example)
  • How to format/parse the date with LocalDateTime in Java 8? (tutorial)
  • 10 Examples to format and parse Date in Java 8? (tutorial)
  • 10 Java Date, Time, and Calendar based Questions from Interviews (questions)
  • How to change the date format of String in Java 8? (tutorial)
  • How to compare two Dates in Java 8? (example)
  • How to convert Timestamp to Date in Java? (example)
  • 20 Examples to learn new Date and Time API in Java 8 (example)
  • Java 8  Parallel Stream Example (Parallel Stream example)
  • 5 Free Courses to learn Java 8 and 9 (courses)
Thanks for reading this article so far. If you like my explanation of the Consumer Functional interface then please share it with your friends and colleagues. If you have any questions or feedback then please drop a comment. 

P. S. - If you are an experienced Java developer and want to master Java, particularly Functional Programming in Java, Streams and Lambdas  and become a Java Guru then you can also check out this list of Top 10 Advanced Core Java Courses on Medium. It contains different Java courses to become an expert Java programmer.

No comments :

Post a Comment