IntStream forEach Method in Java



The forEach() method is used in Java to perform an action for each element of this stream. Display the stream using the forEach() method in Java IntStream

The syntax is as follows

void forEach(IntConsumer action)

Here, action is a non-interfering action to perform on the elements.

Create IntStream and add elements

IntStream intStream = IntStream.of(15, 30, 40, 60, 75, 90);

Now, display the stream

intStream.forEach(System.out::println);

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

Example

 Live Demo

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(15, 30, 40, 60, 75, 90);
      intStream.forEach(System.out::println);
   }
}

Output

15
30
40
60
75
90
Updated on: 2019-07-30T22:30:25+05:30

442 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements