forked from igor-baiborodine/java-various-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupplierInterfaceExample.java
More file actions
45 lines (33 loc) · 958 Bytes
/
SupplierInterfaceExample.java
File metadata and controls
45 lines (33 loc) · 958 Bytes
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
package com.kiroule.ocpupgradejava8.topic2_4;
import java.util.function.Supplier;
/**
* @author Igor Baiborodine
*/
public class SupplierInterfaceExample {
public static void printGreeting(Supplier<HelloWorld> supplier) {
System.out.println(supplier.get().getGreeting());
}
public static void main(String... args) {
Supplier<HelloWorld> supplier = HelloWorld::new;
printGreeting(supplier);
printGreeting(() -> new HelloWorld());
printGreeting(new CustomGreetingSupplier());
}
}
class CustomGreetingSupplier implements Supplier<HelloWorld> {
@Override
public HelloWorld get() {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setGreeting("Custom Hello World!");
return helloWorld;
}
}
class HelloWorld {
private String greeting = "Default Hello World!";
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
}