Java DoubleConsumer Interface with examples

 252 total views,  2 views today

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

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

What is the use of DoubleConsumer?

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

Example

public static void main(String[] args) {

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

    ops.print(1.0);
}
interface Operation
{
    void print(double i);
}

When you look at the above example, we have a function interface with an abstract method which accepts one argument of type double 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 DoubleConsumer interface.

Let now see in-depth details on Java DoubleConsumer Interface with examples.

DoubleConsumer Interface

@FunctionalInterface
public interface DoubleConsumer

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

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

Method description

void accept(double value)

value - the input argument

Example of DoubleConsumer interface using lambda

public static void main(String[] args)
{

    DoubleConsumer consumer = (double i) -> {
        System.out.println(i);
    };

    consumer.accept(1.0);

}

Example of DoubleConsumer interface using Method reference

public static void main(String[] args)
{

    DoubleConsumer consumer = Test::print;
    consumer.accept(1.0);

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

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

DoubleConsumer andThen()

default DoubleConsumer andThen(DoubleConsumer after)

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

Example

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

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

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

}

Output

1.0
1.0me

References

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