A Lambda can be used in any place where a functional interface is required.
A functional interface is any interface with a single abstract method.
The lambda syntax used in this case is (arguments) -> {blockOfCodeOrExpression}
. The parenthesis can be omitted in the case of a single argument, and the braces can be omitted in the case of a single command or expression.
In other words, () -> System.out.println("hello world");
is equivalent* here where a Runnable
is expected to
new Runnable(){
@Override
public void run(){
System.out.println("Hello world one!");
}
};
*(I'm pretty sure that it is not bytecode-equivalent, but is equivalent in terms of functionality)