Looping over a List using forEach (Java 8 style)


From Java8 you can use forEach to iterate over a List, you can see it in action in following example.

import java.util.ArrayList;
import java.util.List;

public class IterateListWithForEach {
	public static void main(String[] args) {
		List<String> colors = new ArrayList<>();
		colors.add("red");
		colors.add("green");
		colors.add("blue");

		colors.forEach((value) -> {
			System.out.println(value);
		});

	}
}

###Output:

red
green
blue

You can see all other possible ways of iterating over a List in this example https://onecompiler.com/posts/3sd56y78n/iterating-on-a-list-in-java-all-possible-ways