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: 2019-07-30T22:30:25+05:30

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements