Quantcast
Channel: programcreek.com » Java 8
Viewing all articles
Browse latest Browse all 6

Enhanced For-loop vs. forEach() in Java 8

$
0
0
Assuming you have the following list: List<String> list = Arrays.asList("a", "b", "c", "d", "e", "f"); if you want to do something by using each element in a collection, there are two ways: 1) list.forEach(s -> System.out.println(s)); 2) for(String s: list){ System.out.println(s); } Both of the above two ways print the elements in sequential: a b […]

Viewing all articles
Browse latest Browse all 6

Trending Articles