LongStream forEach() method in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 27-Mar-2019 08:19:48
Advertisements