Java 8 LongConsumer Example

Java 8 provides built-in consumer interfaces for primitive data types: IntConsumer, LongConsumer and DoubleConsumer.
In this example, we demonstrate the usage of LongConsumer 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 

Comments