Skip to content

Commit 0380a79

Browse files
authored
Create ConsumerForEachEx.java
1 parent 7452e95 commit 0380a79

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

generics/ConsumerForEachEx.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.zetcode;
2+
3+
import java.util.List;
4+
import java.util.function.Consumer;
5+
6+
public class ConsumerForEachEx {
7+
8+
public static void main(String[] args) {
9+
10+
var data = List.of(1, 2, 3, 4, 5, 6, 7);
11+
12+
// Consumer<Integer> consumer = (Integer x) -> System.out.println(x);
13+
Consumer<Integer> consumer = System.out::println;
14+
forEach(data, consumer);
15+
16+
System.out.println("--------------------------");
17+
18+
forEach(data, System.out::println);
19+
}
20+
21+
static <T> void forEach(List<T> data, Consumer<T> consumer) {
22+
23+
for (T t : data) {
24+
consumer.accept(t);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)