-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuncInterface.java
More file actions
111 lines (83 loc) · 2.65 KB
/
Copy pathFuncInterface.java
File metadata and controls
111 lines (83 loc) · 2.65 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
@FunctionalInterface
interface Temp {
void temp(String s);
}
class Person {
String firstName;
String lastName;
Person() {
}
Person(String fname, String lname) {
this.firstName = fname;
this.lastName = lname;
}
public String toString() {
return "Person { firstName: " + firstName + " lastName: " + lastName + " }";
}
}
interface PersonFactory<P extends Person> {
P create(String fname, String lname);
}
public class FuncInterface {
static void funMethod(String s) {
System.out.println("Hello Java" + s);
}
public static void main(String[] args) {
// Example1
Temp temp = (String v1) -> System.out.println("Hello Java" + v1);
temp.temp("5");
// Example2
Temp temp2 = FuncInterface::funMethod;
temp2.temp("10");
// Constructor Example
//The Java compiler automatically chooses the right constructor by
// matching the signature of PersonFactory.create
PersonFactory<Person> personFactory = Person::new;
Person person = personFactory.create("Abrar", "Khan");
System.out.println(person);
// Lambda Scopes
Lambda l = new Lambda();
l.testIt();
System.out.println("Lambda " + l.outerNum + " " + l.outerStaticNum);
// Predicate
PredicateExample.predicate();
// Functional
FunctionsExample.functional();
}
}
class Lambda {
static int outerStaticNum;
int outerNum;
void testIt() {
Temp t = (from) -> {
outerNum = 23;
};
t.temp("Hello");
Temp t1 = (from) -> {
outerStaticNum = 10;
};
t1.temp("Hello");
}
}
class PredicateExample {
public static void predicate() {
Predicate<String> predicate = (s) -> s.length() > 0;
System.out.println("predicate.test() = " + predicate.test("Abrar Khan"));
Predicate<Predicate> predicate1 = Objects::isNull;
System.out.println(predicate1.test(predicate));
Predicate<String> predicate2 = String::isEmpty;
System.out.println(predicate2.negate().test("ehe"));
}
}
class FunctionsExample {
public static void functional() {
Function<String, Integer> toInteger = Integer::valueOf;
System.out.println("Integer Value = " + toInteger.apply("123"));
Function<String, String> backToString = toInteger.andThen(String::valueOf);
System.out.println("backToString.apply(\"123\") = " + backToString.apply("123"));
}
}