Skip to main content

Introduction to Java 8 in Layman's terms - Functional Interface - Consumer Interface

Consumer Interface

What is consumer interface ?

Consumer interface is one of the functional interfaces that Java 8 provides. It is present in java.util.function package.

    @FunctionalInterface

     public interface consumer<T>

Let us look at some of the important points about the consumer interface below.

  • Consumer interface is a functional interface that is it only has on method definition and that method is called the accept . It takes in only one parameter and does not return anything as the output.
                public void accept(T t)
               
  • We also know that functional interfaces can have method of default scope also that is default methods. Well Consumer interface also has one default method know as andThen which takes in a consumer as input parameter.
             default Consumer<T> andThen(Cosumer<? super T> after)

        This method is used to link the consumers together . That is to make one consumer run after other.

Let us look at few examples !!!

Let us first look at how to give a input to the consumer interface and perform an operation. For giving an input to the consumer interface we can make use of the accept method as shown below .

    Class ConsumerExample{

        public static void main(String[] args){

              Consumer<String> consumer= (s)->System.out.println(s);

              consumer.accept("Example test");

        }

   }

   o/p = Example test

Let us understand what we are doing in above example. We are creating an Consumer interface of string type. Then we are using lambda expression to pass a string as input to the accept method and we are defining the body of the accept method to print the passed string .

If you are finding it difficult to understand Lambda expression then please refer to the link below .

https://praveen075.blogspot.com/2021/04/introduction-to-java-8-in-laymans-terms.html


Now let us look at another example where we make use of andThen method to call one consumer after another. 


    Class ConsumerExample{

        public static void main(String[] args){

              Consumer<String> consumer1= (s)->System.out.println(s);

              Consumer<String> consumer2= (s)->System.out.println(s.toUpperCase());

              consumer1.andThen(consumer2).accept("Example test");

        }

   }

     o/p :

     Example test

     EXAMPLE TEST


Let us look at above example. 

First we are creating one consumer called consumer1 which takes in a string and just prints it. 

Next we are creating one more consumer called consumer2 which takes in a string converts the string to uppercase and then prints the string.

Next we want to make consumer1 run first and then we want to make consumer2 run. 

So we do consumer1.andThen(consumer2) which achieves that .

Next we need to pass some input to perform this operation so we make use of accept method of consumer as below .

consumer1.andThen(consumer2).accept("Example test")


Note: Kindly correct me in any info that is wrong on above blog . I am happy to learn .

Thank you


Comments

Popular posts from this blog

Introduction to Java 8 in Layman's terms - Bi Consumer interface

Bi-Consumer interface   What is Bi-Consumer interface ? Bi-Consumer interface is one of the functional interfaces that Java 8 provides. It is present in java.util.function package. It is quite similar to consumer interface, just that it accepts two parameters.     @FunctionalInterface     public interface BiConsumer<T,U> Since it is a functional interface it has only one method definition . That method is called Accept. We all know functional interfaces can have default methods also . So here we have a default method called andThen . What are the methods of the Bi-Consumer interface ? Accept :      void accept(T t,U u) The accept method accepts two input parameter and performs a user defined operation on them. AndThen :    default BiConsumer<T,U> andThen(BiConsumer<? super T,? super U> after) The andThen method takes in a Bi-Consumer as an input and it runs it after the Bi-Consumer it is called upon. Let us look at few examples !!!!!!  First lets look at the example

Introduction to Java 8 in Layman's terms - Lambda expression & Functional interfaces basics

Lambda expression  & Functional interfaces basics What is Lambda expression ? To put it in simple terms Lambda expression is a short hand form of functions, more specifically anonymous functions. Anonymous functions are something that is not associated to a class . That is exactly what Lambda expressions are also. How do we write a Lambda expression ? Well there are 3 main sections of an :  (     )                  ->             { }  Input             Arrow          Body parameters Below let us look at individual sections of a  Lambda expression. Input parameters - This section is similar to input parameter section of a method where we provide input parameters. This section usually is surrounded by () brackets. It can have zero or more parameters .  Arrow or Arrow operator - Arrow is something that indicates the flow towards the body of the Lambda expression. IT pretty much just denotes the end of input parameters section and beginning of the body. Body - Body is where we pro