IntConsumer Interface in Java with Examples


The IntConsumer Interface is a functional interface that represents an operation that accepts a single int-valued argument and returns no result. This is the int-consuming primitive specialization of the Consumer interface. Here, the functional interface means an interface that contains only a single abstract method and exhibits single functionality. Some examples of functional interfaces are Predicate, Runnable, and Comparable interfaces. In this article, we are going to explore the IntConsumer Interface and its built-in methods with the help of example programs.

IntConsumer Interface in Java

In Java, the IntConsumer Interface provides only two methods:

  • accept()

  • andThen()

We will discuss them one by one, but before that let's see the syntax of IntConsumer Interface.

Syntax

public interface IntConsumer

It is necessary to import this Interface before using it in our program. To import the IntConsumer Interface use the following command:

import java.util.function.IntConsumer;

Use of accept() method of IntConsumer

The 'accept()' method is a built-in abstract method of IntConsumer Interface that accepts a single input of integer type and returns nothing. Hence, we need to use the standard output method so that we can see the result.

Syntax

instance.accept(int val)

Here, the 'instance' specifies the instance of IntConsumer and 'val' specify the operand on which the operation will be performed.

Example 1

The following example demonstrates the use of 'accept()' method in printing the square of a specified integer variable.

Approach

  • First, import the required package that was mentioned earlier.

  • Then, create an instance of IntConsumer that will return the square of specified integer variable.

  • In the end, use the 'accept()' method along with the instance of IntConsumer and pass the required integer value to perform the square operation.

import java.util.function.IntConsumer;
public class Example1 {
   public static void main(String[] args) {
      // creating an instance of IntConsumer
      IntConsumer printSquare = x -> System.out.println("Square of specified value: " + x * x);
      // to print the result
      printSquare.accept(5); 
      printSquare.accept(10);     
   }
}

Output

Square of specified value: 25
Square of specified value: 100

Use of andThen() method of IntConsumer

The 'andThen()' method is the default method of IntConsumer Interface that chains multiple operations of IntConsumer sequentially and returns a composed IntConsumer of those operations. It displays the results in the same order in which they are defined.

Syntax

firstInstance.andThen(secondInstance)

Example 2

The following example illustrates how to use the 'andThen()' method of IntConsumer.

Approach

  • The first step is to import the 'java.util.function.IntConsumer' so that we can use its methods.

  • Create two instances of IntConsumer, one for calculating square and the other one for calculating cube.

  • Use the 'andThen()' method to chain both operations so that the cube operation gets executed after the square.

  • In the end, use the 'accept()' method along with the instance of IntConsumer and pass the required integer value to start the operation.

import java.util.function.IntConsumer;
public class Example2 {
   public static void main(String[] args) {
      // creating instances of IntConsumer
      IntConsumer printSquare = x -> System.out.println("Square of specified value: " + x * x);
      IntConsumer printCube = x -> System.out.println("Cube of specified value: " + x * x * x);
      // use of andThen() method
      IntConsumer printResult = printSquare.andThen(printCube);
      // to print the result
      printResult.accept(5); 
      printResult.accept(3); 
   }
}

Output

Square of specified value: 25
Cube of specified value: 125
Square of specified value: 9
Cube of specified value: 27

Conclusion

In this article, we have learned about the IntConsumer Interface and its built-in methods. It has only two methods namely 'accept()' and 'andThen()'. The 'accept()' method takes an integer argument and returns nothing whereas the 'andThen()' method returns a composed IntConsumer of specified operation.

Updated on: 20-Jul-2023

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements