Java 8 DoubleConsumer Example

Java 8 provides built-in consumer interfaces for primitive data types: IntConsumer, LongConsumer and DoubleConsumer.
In this example, we demonstrate the usage of DoubleConsumer interface.

Java 8 DoubleConsumer Example

In the example, we create the DoubleConsumer object and iterate over them with forEach().
import java.util.Arrays;
import java.util.function.DoubleConsumer;

public class JavaForEachConsSpec {

    public static void main(String[] args) {
        double[] dnums = { 3.4d, 9d, 6.8d, 10.3d, 2.3d };
        DoubleConsumer dcons = d -> System.out.print(d + " ");
        Arrays.stream(dnums).forEach(dcons);
        
    }
}
Output:
3.4 9.0 6.8 10.3 2.3 



Comments