Skip to content

Java 8 LongConsumer Example

Ramesh Fadatare edited this page Jul 11, 2019 · 1 revision

Java 8 provides built-in consumer interfaces for primitive data types: IntConsumer, LongConsumer and DoubleConsumer.

In this example, we demonstrates the usage of DoubleConsumer interface.

Java 8 LongConsumer Example

In the example, we create the LongConsumer object and iterate over them with forEach().

import java.util.Arrays;
import java.util.function.LongConsumer;

public class JavaForEachConsSpec {

    public static void main(String[] args) {

        long[] lnums = { 13l, 3l, 6l, 1l, 8l };
        LongConsumer lcons = l -> System.out.print(l + " ");
        Arrays.stream(lnums).forEach(lcons);
        
    }
}

Output:

13 3 6 1 8 
Clone this wiki locally