Java IntConsumer Interface with examples

 224 total views,  1 views today

Welcome to Java 8 Series. In this tutorial, you will learn about the Java IntConsumer Interface with examples.

IntConsumer is a Functional interface introduced as part of Java 8. It is available in java.util.function package.

What is the use of IntConsumer?

Consider a case where we need to write a lambda expression which accepts one input of type integer and perform an operation on that input and return no result.

Example

public static void main(String[] args)
{

    Operation ops = (int i) -> {
        System.out.println(i);
    };

    ops.print(1);
}
interface Operation
{
    void print(int i);
}

When you look at the above example, we have a function interface with an abstract method which accepts one argument of type integer and returns void. So a lambda expression needs a target type which needs to be a functional interface and in this example, it is Operation interface.

Instead of creating our own function interface for this scenario, Java has provided us an IntConsumer interface. Let now see in-depth details on Java IntConsumer Interface with examples.

IntConsumer Interface

@FunctionalInterface
public interface IntConsumer

IntConsumer interface contains an operation that accepts one input argument of type integer and returns no result.

It is a functional interface and the functional method is accept(int).

Method description

void accept(int value)
value - the input argument

Example of IntConsumer interface using lambda

public static void main(String[] args)
{
    IntConsumer consumer = (int i) -> {
        System.out.println(i);
    };
    consumer.accept(1);
}

Example of IntConsumer interface using Method reference

public static void main(String[] args)
{
    IntConsumer consumer = Test::print;
    consumer.accept(1);
}
static void print(int i) {
    System.out.println(i);
}

If you want to write a lambda expression with one argument of type integer and no result, please use Java IntConsumer interface.

IntConsumer andThen()

default IntConsumer andThen(IntConsumer after)

It is a default method which accepts an IntConsumer.This will be executed after the current IntConsumer is executed.

Example

public static void main(String[] args)
{
    IntConsumer consumer = (int i) -> {
        System.out.println(i);
    };

    IntConsumer consumerObj = (int i) -> {
        System.out.println(i + "me");
    };

    consumer.andThen(consumerObj).accept(1);

}

Output

1
1me

References

Java 8 IntConsumer official documentation.

Thanks for reading. Please a comment and give us a thumbs up.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.