IntConsumer Interface in Java with Examples

The IntConsumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one int-valued argument but does not return any value.

The lambda expression assigned to an object of IntConsumer type is used to define its accept() which eventually applies the given operation on its only argument.It is similar to using an object of type Consumer<Integer>

The IntConsumer interface consists of the following two functions:



accept()

This method accepts one value and performs the operation on its only argument.

Syntax:

void accept(int value)

Parameters: This method takes in only one parameter:

  • value– the input argument

Returns: This method does not return any value.

Below is the code to illustrate accept() method:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.function.IntConsumer;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Create a IntConsimer Instance
        IntConsumer display = a -> System.out.println(a * 10);
  
        // Using accept() method
        display.accept(3);
    }
}

chevron_right


Output:

30

andThen()

It returns a composed IntConsumer wherein the parameterized IntConsumer will be executed after the first one. If the evaluation of either operation throws an error, it is relayed to the caller of the composed operation.

Note: The operation being passed as the argument should be of type IntConsumer.

Syntax:

default IntConsumer andThen(IntConsumer after)

Parameters: This method accepts a parameter after which is the IntConsumer to be applied after the current one.


Return Value: This method returns a composed IntConsumer that first applies the current operation first and then the after operation.

Exception: This method throws NullPointerException if the after operation is null.

Below programs illustrate andThen() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.function.IntConsumer;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Create a IntConsimer Instance
        IntConsumer display = a -> System.out.println(a * 10);
        IntConsumer mul = a -> a *= 10;
  
        // Using andThen() method
        IntConsumer composite = mul.andThen(display);
        composite.accept(3);
    }
}

chevron_right


Output:

30

Program 2: To demonstrate when NullPointerException is returned.

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.function.IntConsumer;
  
public class GFG {
    public static void main(String args[])
    {
  
        try {
  
            // Create a IntConsimer Instance
            IntConsumer mul = a -> a *= 10;
            IntConsumer composite = mul.andThen(null);
            composite.accept(3);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}

chevron_right


Output:

Exception : java.lang.NullPointerException

Program 3: To demonstrate how an Exception in the after function is returned and handled.

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.function.IntConsumer;
  
public class GFG {
    public static void main(String args[])
    {
  
        try {
            // Create a IntConsimer Instance
            IntConsumer divide = a -> a = a / (a - 3);
            IntConsumer mul = a -> a *= 10;
            IntConsumer composite = mul.andThen(divide);
            composite.accept(3);
        }
  
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}

chevron_right


Output:

Exception : java.lang.ArithmeticException: / by zero


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.