-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_Consumer.java
59 lines (43 loc) · 1.69 KB
/
_Consumer.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
package com.umer.javafunctional.functionalinterface;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class _Consumer {
public static void main(String[] args) {
// Normal java function
final Customer maria = new Customer("Maria", "99999");
greetCustomer(maria);
greetCustomerV2(maria, false);
// Consumer Functional interface
greetCustomerConsumer.accept(maria);
greetCustomerConsumerV2.accept(maria, false);
}
// Declarative approach
static Consumer<Customer> greetCustomerConsumer=customer ->System.out.println(
"Hello " + customer.customerName
+ ", thanks for registering phone number "
+ customer.customerPhoneNumber);
static BiConsumer<Customer, Boolean> greetCustomerConsumerV2=(customer, showPhoneNumber) -> System.out.println(
"Hello " + customer.customerName
+ ", thanks for registering phone number "
+ (showPhoneNumber ? customer.customerPhoneNumber: "********"));
// Imperative approach
static void greetCustomer(Customer customer) {
System.out.println("Hello " + customer.customerName
+ ", thanks for registering phone number "
+ customer.customerPhoneNumber);
}
static void greetCustomerV2(Customer customer, Boolean showPhoneNumber) {
System.out.println("Hello " + customer.customerName
+ ", thanks for registering phone number "
+ (showPhoneNumber ? customer.customerPhoneNumber: "********"));
}
static class Customer{
private final String customerName;
private final String customerPhoneNumber;
public Customer(String customerName, String customerPhoneNumber) {
super();
this.customerName = customerName;
this.customerPhoneNumber = customerPhoneNumber;
}
}
}