Lambda/Closure7JDK8 Lambda
English
Google Play Pubu Java Lambda Tutorial
JDK8 Lambda JDK8
(x -> x * 2)
(Integer x) -> x + 2
Java
Lambda/Closure4 Type inference Lambda/Closure JDK8
Lambda/Closure6
JDK8 Lambda
compose
public static <A, B, C> Func<A, C> compose(Func<A, B> f, Func<B, C> g) {
return x -> g.apply(f.apply(x));
}
Lambda/Closure6 Func "
.apply"
x -> g.apply(f.apply(x)) x -> g(f(x)) compose g(f(x)) f(x) = x + 2 g(y) = y * 3
compose((Integer x) -> x + 2, (Integer y) -> y * 3);
Lambda/Closure6
compose(
new Func<Integer, Integer>() {
public Integer apply(Integer x) {
return x + 2;
}
},
new Func<Integer, Integer>() {
public Integer apply(Integer y) {
return y * 3;
}
}
);
JDK8 Lambda
token -> Lambda
// x y
(int x, int y) -> x + y
// 42
() -> 42
JDK8 Lambda
//
(String s) -> { out.println(s); }
//
(Integer x) -> {
Integer result;
...other statements
...
return result;
};
Lambda JDK8 Lambda
Lambda JavaScript
Function JDK8 Lambda Target typeLambda Lambda Java
Lambda/Closure5 API Java Lambda JDK8 Functional interface Lambda
RunnableCallableComparator
public interface Runnable {
void run();
}
public interface Callable<V> {
V call() throws Exception;
}
public interface Comparator<T> {
int compare(T o1, T o2);
}
Lambda/Closure6 Func Lambda Lambda
Func
Func<Integer, Integer> func = x -> x * 2;
x Method signature Lambda
public interface Function<P, R> {
R call(P p);
}
(x -> x * 2) Function Double
Function<Double, Double> f2 = x -> x * 2;
Lambda
JDK8
@FunctionalInterface
@FunctionalInterface
public interface Func<P, R> {
R apply(P p);
}
@FunctinalInterface
@FunctionalInterface
public interface Function<P, R> {
R call(P p);
R call(P p1, P p2);
}
>@FunctionalInterface
^
Function is not a functional interface
multiple non-overriding abstract methods found in interface Function
Lambda
import static java.lang.System.out;
public class Hello {
Runnable r1 = new Runnable() {
public void run() {
out.println(this);
}
};
Runnable r2 = new Runnable() {
public void run() {
out.println(toString());
}
};
public String toString() { return "Hello, world!"; }
public static void main(String[] args) {
new Hello().r1.run();
new Hello().r2.run();
}
}
Hello$1@103368e Hello$2@1f2ae62
this toString
import static java.lang.System.out;
public class Hello {
Runnable r1 = () -> { out.println(this); };
Runnable r2 = () -> { out.println(toString()); };
public String toString() { return "Hello, world!"; }
public static void main(String[] args) {
new Hello().r1.run();
new Hello().r2.run();
}
}
"Hello, world!"Lambda
this toString Hello compose final Lambda/Closure5 Java
finalJDK8
final Lambda
final
Lambda JavaScript Scala Free variableJDK8 Lambda JDK8 Lambda
JDK8 Lambda API Java Lambda Java API