Wednesday 30 October 2019

Consumer Interface

The Consumer Interface is a part of the java.util.function package which has been introduced in Java 8, to implement functional programming in Java. It represents a function which takes in one argument and produces a result. However these kind of functions don’t return any value.
  1. Consumer Functional Interface contains one abstract method accept. 
  2. Sometimes our requirment is we have to provide some input value, perform certain operation.
  3. But not required to return anything,then we should go for Consumer.i.e Consumer can be used to consume object and perform certain operation.
  4. Consumer interface contain one abstract method is accept().
  5. Consumer Functional Interface contains default method andThen().
Syntax:
       
              interface Consumer<T>   
              {
                         public void accept(T t);   
              }

             T-the type of the input to the operation.
             t- the input argument.
           
Example:1. How to display input value using Consumer Functional interface.


Output:
Hello
Hello Sudhir

Share this

0 Comment to "Consumer Interface"

Post a Comment