LongStream forEach() method in Java


The forEach() method of the LongStream class in Java performs an action for each element of this stream.

The syntax is as follows

void forEach(LongConsumer action)

Here, the parameter action is a non-interfering action to perform on the elements. LongConsumer represents an operation that accepts a single long-valued argument and returns no result.

To use the LongStream class in Java, import the following package

import java.util.stream.LongStream;

The following is an example to implement LongStream forEach() method in Java

Example

 Live Demo

import java.util.*;
import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(1000L, 12000L, 30000L);
      longStream.forEach(System.out::println);
   }
}

Output

1000
12000
30000

Updated on: 30-Jul-2019

103 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements