We would like to know how to compare forEach implementation.
/*from w ww . j a v a 2 s .c o m*/ import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<String> friends = Arrays.asList("A", "B", "C", "D"); //external iterate with index == evil for(int i = 0; i < friends.size(); i++) { System.out.println(friends.get(i)); } //external iterate on collection, better but ... external for(String friend : friends){ System.out.println(friend); } //lambda friends.forEach(s -> System.out.println(s)); //lambda reference friends.forEach(System.out::println); } }
The code above generates the following result.