Monday, 6 May 2019

Java 8 - Consumer Interface with examples

        In the current post, I will explain you the in-built functional interface Consumer introduced in Java 8 with examples. It contains two methods accept() & andThen(), comes under java.util.function package.

What is java.util.function.Consumer ?

       Consumer<T> is an in-built functional interface introduced in Java 8 and it is in the java.util.function package.  Here T is any type of input arguments. Consumer can be used in all contexts where an object needs to be consumed, i.e it will take argument as an input and some operation needs to be performed on the object without returning any result.  

       This is a functional interface because it contains only one abstract method i.e accept().

Methods in Consumer Interface

  • accept()
  • andThen()

       The accept() method is abstract method, need to implement in the code, it will take one parameter as argument and doesn't return anything.

syntax:-

    void accept(T t);

The following example shows how to use the accept() method of the Consumer interface.

package com.test;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerInterface {
 
       public static void main(String[] args) {
  
               List<Integer> values = Arrays.asList(2,3,5,8);
               Consumer<Integer> printInt = new Consumer<Integer>() {
                        @Override
                        public void accept(Integer var) {
                                  System.out.println("Number - "+var);
                        }  
               };
  
               //print the single integer number using consumer interface.
               printInt.accept(10);
  
               //print the integers using consumer interface
               values.forEach(printInt);
       }

}

Output:--
Number - 10
Number - 2
Number - 3
Number - 5
Number - 8


andThen() -   is a default method in Consumer interface. Method andThen(), when applied on a Consumer interface, takes as input another instance of Consumer interface and returns as a result a new consumer interface which represents aggregation of both of the operations defined in the two Consumer interfaces.

syntax:-

default Consumer <T> 
        andThen(Consumer<? super T> after)

The return type of the above method is Consumer and this accepts a parameter after which is the Consumer to be applied after the current one.

The below code is to illustrate the andThen() method,

package com.test;

import java.util.function.Consumer;

public class ConsumerInterface {
 
      public static void main(String[] args) {
  
              Consumer<String> cons1 = str -> {
                        System.out.println(str + " World");
              };
      
              Consumer<String> cons2 = str -> {
                        System.out.println(str + " Java");
              };
  
              cons1.andThen(cons2).accept("Hello");
      }

}

Output:-

Hello World
Hello Java



Related Posts:-
1) Java 8 forEach method with examples
2) Java 8 features with examples

No comments:

Post a comment