6 Ways To Use forEach in Java
August 23, 2020To use for each in Java, you need to use the syntax like
1 2 3 |
iterableObject.forEach(element -> System.out.println(element)); |
Let’s have a look at some more examples of forEach in Java. 1. forEach and list Using forEach, you can conveniently iterate over elements in a list. Have a look at the example below
1 2 3 4 5 6 7 8 |
List<Integer> integerList = new LinkedList<>(); integerList.add(1); integerList.add(2); integerList.forEach(number -> System.out.println(number)); integerList.forEach(System.out::println); |
The last two lines show […]