Lambda Expression 定义
原创转载请注明出处:http://agilestyle.iteye.com/blog/2424764
A lambda expression can be understood as a concise representation of an anonymous function that can be passed around: it doesn’t have a name, but it has a list of parameters, a body, a return type, and also possibly a list of exceptions that can be thrown. That’s one big definition;
let’s break it down:
- Anonymous— We say anonymous because it doesn’t have an explicit name like a method would normally have: less to write and think about!
- Function— We say function because a lambda isn’t associated with a particular class like a method is.But like a method, a lambda has a list of parameters, a body, a return type, and a possible list of exceptions that can be thrown.
- Passed around— A lambda expression can be passed as argument to a method or stored in a variable.
- Concise— You don’t need to write a lot of boilerplate like you do for anonymous classes.
Example 1 —— ThreadTest.java
package org.fool.java8;
public class ThreadTest {
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName());
}
}).start();
new Thread(() -> {
System.out.println(Thread.currentThread().getName());
}).start();
new Thread(() -> System.out.println(Thread.currentThread().getName())).start();
}
}
Reference
Manning.Java.8.in.Action.Lambdas.Streams.and.functional-style.programming.Aug.2014