Stream forEach() method in Java with examples
Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect.
Syntax :
void forEach(Consumer<? super T> action) Where, Consumer is a functional interface and T is the type of stream elements.
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 : To perform print operation on each element of reversely sorted stream.
// Java code for forEach // (Consumer action) in Java 8 import java.util.*; class GFG { // Driver code public static void main(String[] args) { // Creating a list of Integers List<Integer> list = Arrays.asList(2, 4, 6, 8, 10); // Using forEach(Consumer action) to // print the elements of stream // in reverse order list.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println); } } |
10 8 6 4 2
Example 2 : To perform print operation on each element of string stream.
// Java code for forEach // (Consumer action) in Java 8 import java.util.*; class GFG { // Driver code public static void main(String[] args) { // Creating a list of Strings List<String> list = Arrays.asList("GFG", "Geeks", "for", "GeeksforGeeks"); // Using forEach(Consumer action) to // print the elements of stream list.stream().forEach(System.out::println); } } |
GFG Geeks for GeeksforGeeks
Example 3 : To perform print operation on each element of reversely sorted string stream.
// Java code for forEach // (Consumer action) in Java 8 import java.util.*; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // Creating a Stream of Strings Stream<String> stream = Stream.of("GFG", "Geeks", "for", "GeeksforGeeks"); // Using forEach(Consumer action) to print // Character at index 1 in reverse order stream.sorted(Comparator.reverseOrder()) .flatMap(str -> Stream.of(str.charAt(1))) .forEach(System.out::println); } } |
o e e F
Recommended Posts:
- CopyOnWriteArraySet forEach() method in Java with Examples
- LinkedBlockingDeque forEach() method in Java with Examples
- LinkedTransferQueue forEach() method in Java with Examples
- Iterable forEach() method in Java with Examples
- HashTable forEach() method in Java with Examples
- CopyOnWriteArrayList forEach() method in Java with Examples
- Flatten a Stream of Map in Java using forEach loop
- Properties forEach(BiConsumer) method in Java with Examples
- Flatten a Stream of Lists in Java using forEach loop
- Flatten a Stream of Arrays in Java using forEach loop
- Stream min() method in Java with Examples
- Stream.max() method in Java with Examples
- BitSet stream() Method in Java with Examples
- Stream count() method in Java with examples
- Stream generate() method in Java with examples
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.



