IntStream forEach() method in Java
IntStream forEach(IntConsumer action) performs an action for each element of the stream. IntStream forEach(IntConsumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect.
Syntax :
void forEach(IntConsumer action)
Parameter : IntConsumer represents an operation that accepts a single int-valued argument and returns no result. This is the primitive type specialization of Consumer for int.
Note : The behavior of this operation is explicitly nondeterministic. Also, for any given element, the action may be performed at whatever time and in whatever thread the library chooses.
Example 1 :
// Java code for IntStream forEach // (IntConsumer action) in Java 8 import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating an IntStream IntStream stream = IntStream.of(7, 8, 9, 10); // Using IntStream.forEach stream.forEach(System.out::println); } } |
7 8 9 10
Example 2 :
// Java code for IntStream forEach // (IntConsumer action) in Java 8 import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating an IntStream IntStream stream = IntStream.range(4, 9); // Using IntStream.forEach() on sequential stream stream.forEach(System.out::println); } } |
4 5 6 7 8
Note : For parallel stream, IntStream forEach(IntConsumer action) does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. Below is the example.
Example 3 :
// Java code for IntStream forEach // (IntConsumer action) in Java 8 import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating an IntStream IntStream stream = IntStream.range(4, 9); // Using IntStream.forEach() on parallel stream stream.parallel().forEach(System.out::println); } } |
6 8 7 5 4
Recommended Posts:
- ArrayList forEach() method in Java
- Vector forEach() method in Java
- LongStream forEach() method in Java
- DoubleStream forEach() method in Java
- ArrayDeque forEach() method in Java
- Stream forEach() method in Java with examples
- HashTable forEach() method in Java with Examples
- Iterable forEach() method in Java with Examples
- CopyOnWriteArrayList forEach() method in Java with Examples
- LinkedTransferQueue forEach() method in Java with Examples
- CopyOnWriteArraySet forEach() method in Java with Examples
- LinkedBlockingDeque forEach() method in Java with Examples
- Properties forEach(BiConsumer) method in Java with Examples
- IntStream forEachOrdered() method in Java
- IntStream.Builder add() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



