-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathConsumerInterfaceExample.java
42 lines (36 loc) · 1.4 KB
/
ConsumerInterfaceExample.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
package javaConsumerInterfaceExamples;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class ConsumerInterfaceExample {
static void printMessage(String name) {
System.out.println("Hello " + name);
}
static void printValue(int val) {
System.out.println(val);
}
static void addList(List<Integer> list) {
// Return sum of list values
int result = list.stream()
.mapToInt(Integer::intValue)
.sum();
System.out.println("Sum of list values: " + result);
}
public static void main(String[] args) {
// Referring method to String type Consumer interface
Consumer<String> consumer1 = ConsumerInterfaceExample::printMessage;
consumer1.accept("John"); // Calling Consumer method
// Referring method to Integer type Consumer interface
Consumer<Integer> consumer2 = ConsumerInterfaceExample::printValue;
consumer2.accept(12); // Calling Consumer method
// Creating a list and adding values
List<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
// Referring method to String type Consumer interface
Consumer<List<Integer>> consumer = ConsumerInterfaceExample::addList;
consumer.accept(list); // Calling Consumer method
}
}