Java LongConsumer Interface with examples

 197 total views,  2 views today

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

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

What is the use of LongConsumer?

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

Example

public static void main(String[] args)
{

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

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

When you look at the above example, we have a function interface with an abstract method which accepts one argument of type long 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 a LongConsumer interface. Let now see in-depth details on Java LongConsumer Interface with examples.

LongConsumer Interface

@FunctionalInterface
public interface LongConsumer

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

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

Method description

void accept(long value)
value - the input argument

Example of LongConsumer interface using lambda

public static void main(String[] args)
{

    LongConsumer consumer = (long i) -> {
        System.out.println(i);
    };

    consumer.accept(1);

}

Example of LongConsumer interface using Method reference

public static void main(String[] args)
{

    LongConsumer consumer = Test::print;
    consumer.accept(1);

}
static void print(long i) {
    System.out.println(i);
}

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

LongConsumer andThen()

default LongConsumer andThen(LongConsumer after)

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

Example

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

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

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

}

Output

1
1me

References

Java 8 LongConsumer 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!.