-
Notifications
You must be signed in to change notification settings - Fork 0
/
Iteration.java
64 lines (43 loc) · 1.67 KB
/
Iteration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17+
import java.util.List;
import java.util.function.Consumer;
public class Iteration {
public static void main(String[] args) {
final List<String> developers = List.of(
"Maximillian",
"Otavio Santana",
"Bruno Souza",
"Elder Moraes",
"Sérgio Lopes",
"Fernando Boaglio");
for (int i = 0; i < developers.size(); i++) {
System.out.println(developers.get(i));
}
System.out.println();
// another way to iterate using imperative style
for (String developer : developers) {
System.out.println(developer);
}
System.out.println();
// using forEach
developers.forEach(new Consumer<String>() {
@Override
public void accept(final String developer) {
System.out.println(developer);
}
});
System.out.println();
// using forEach with Lambda Expression
developers.forEach((final String developer) -> System.out.println(developer));
System.out.println();
// using forEach with Lambda Expression + type inference
developers.forEach((developer) -> System.out.println(developer));
System.out.println();
// using forEach with Lambda Expression + type inference + without parentheses
developers.forEach(developer -> System.out.println(developer));
System.out.println();
// using forEach with Method References
developers.forEach(System.out::println);
}
}