-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_Consumer.java
More file actions
60 lines (46 loc) · 1.69 KB
/
Copy path_Consumer.java
File metadata and controls
60 lines (46 loc) · 1.69 KB
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
package functionalprogramming;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class _Consumer {
/*
Consumer accepts a single argument and returns no result.
A BiConsumer accepts two arguments and returns no result.
It's basically a void function.
We use the ".accept()" method with Consumers.
*/
static Consumer<Customer> greetThem = customer ->
System.out.println("Hello " + customer.name + " and here's my number " +
customer.phoneNum);
static BiConsumer<Customer, Customer> meetCustomers =
(oneCustomer, twoCustomer) ->
System.out.println(oneCustomer.name + " is friends with "
+ twoCustomer.name);
public static void main(String[] args) {
Customer stefan = new Customer("Stefan", "813-809-2847");
greetCustomer(stefan);
Customer vince = new Customer("Vince", "305-445-5555");
// Functional
greetThem.accept(stefan); // same output as above
// BiFunctional example
meetCustomers.accept(stefan, vince);
}
static void greetCustomer(Customer customer) {
System.out.println("Hello " + customer.name + " and here's my number " +
customer.phoneNum);
}
static class Customer {
private final String name;
private final String phoneNum;
Customer(String name, String phoneNum) {
this.name = name;
this.phoneNum = phoneNum;
}
}
/**
* Output:
*
* Hello Stefan and here's my number 813-809-2847
* Hello Stefan and here's my number 813-809-2847
* Stefan is friends with Vince
*/
}