Back to Java Course

Java Course

  • This method will not return anything.
  • This method will take lambda expression as argument and apply that lambda expression for each element present in the stream.

For example,

l.stream().forEach(s->System.out.println(s)); l3.stream().forEach(System.out::println);

Let's see another example,

import java.util.Arrays; import java.util.List; public class ForEachDemo { public static void main(String args[]) { withoutForEach(); withForEach(); } private static void withoutForEach() { System.out.println("From withoutForEach"); List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9); for (int i : intList) { System.out.println(i); } } private static void withForEach() { System.out.println("From withForEach"); List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9); intList.forEach(System.out::println); } }

Output

From withoutForEach 1 2 3 4 5 6 7 8 9 From withForEach 1 2 3 4 5 6 7 8 9