-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample163.java
More file actions
96 lines (84 loc) · 4.08 KB
/
Example163.java
File metadata and controls
96 lines (84 loc) · 4.08 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
// Example 163 from page 131
//
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.stream.Stream;
class Example163 {
public static void main(String[] args) {
compileTimeLegalAndIllegal();
defineFunction();
defineToIntFunction();
defineVarargFunction();
}
private static void defineVarargFunction() {
// Three equivalent ways of defining a lambda with variable number of arguments
VarargFunction<String,String>
fsas1 = ss -> String.join(":", ss),
fsas2 = (String[] ss) -> String.join(":", ss),
fsas3 = (String... ss) -> String.join(":", ss);
System.out.println(fsas1.apply("abc", "DEF"));
System.out.println(fsas2.apply("abc", "DEF"));
System.out.println(fsas3.apply("abc", "DEF"));
// A derived interface has also the superinterface's default
// methods but its static methods:
System.out.println(fsas3.andThen(String::length).apply(new String[] { "abc", "DEF" }));
}
private static void defineFunction() {
Function<String,Integer>
fsi1 = s -> Integer.parseInt(s), // lambda with parameter s
fsi2 = (String s) -> Integer.parseInt(s), // same, with explicit parameter type
fsi3 = Integer::parseInt, // reference to static method Integer.parseInt
fsi4 = Integer::new, // reference to constructor Integer(String)
fsi5 = s -> s.length(), // lambda with parameter s
fsi6 = String::length, // reference to instance method s.length()
fsi7 = new Function<String,Integer>() { // anonymous inner class (Java 1.1)
public Integer apply(String s) {
return s.length();
}};
applyFsi(fsi1);
Stream<Function<String,Integer>> fsis = Stream.of(fsi1, fsi2, fsi3, fsi4, fsi5, fsi6, fsi7 );
fsis.map(fsi -> fsi.apply("4711")).forEach(System.out::println);
}
private static void applyFsi(Function<String,Integer> fsi) {
int res = fsi.apply("4711");
System.out.println(res);
}
private static void defineToIntFunction() {
ToIntFunction<String>
fsi1 = s -> Integer.parseInt(s), // lambda with parameter s
fsi2 = (String s) -> Integer.parseInt(s), // same, with explicit parameter type
fsi3 = Integer::parseInt, // reference to static method Integer.parseInt
fsi4 = Integer::new, // reference to constructor Integer(String)
fsi5 = s -> s.length(), // lambda with parameter s
fsi6 = String::length, // reference to instance method s.length()
fsi7 = new ToIntFunction<String>() { // anonymous inner class (Java 1.1)
public int applyAsInt(String s) {
return s.length();
}};
applyFsi(fsi1);
Stream<ToIntFunction<String>> fsis = Stream.of(fsi1, fsi2, fsi3, fsi4, fsi5, fsi6, fsi7 );
fsis.map(fsi -> fsi.applyAsInt("4711")).forEach(System.out::println);
Function<String,Integer> f = s -> Integer.parseInt(s);
// Type error: Function<String,Integer> cannot be converted to ToIntFunction<String>
// ToIntFunction<String> g = f;
}
private static void applyFsi(ToIntFunction<String> fsi) {
int res = fsi.applyAsInt("4711");
System.out.println(res);
}
private static void compileTimeLegalAndIllegal() {
// Illegal: Double::toString is not in assignment, argument or casting context
// Function<Double,Integer> fdi1 = Double::toHexString.andThen(String::length);
// Legal: Double::toString is in casting context
Function<Double,Integer> fdi2
= ((Function<Double,String>)Double::toHexString).andThen(String::length);
System.out.println(fdi2.apply(1.1));
// Illegal: No andThen method on DoubleFunction<String>
// Function<Double,Integer> fdi3 = ((DoubleFunction<String>)Double::toHexString).andThen(String::length);
}
@FunctionalInterface
@SuppressWarnings("unchecked")
interface VarargFunction<T,R> extends Function<T[],R> {
public abstract R apply(T... xs);
}
}