Single Parameter
Description
For a single parameter lambda expression we can omit the parentheses as we omit the parameter type.
The lambda expression
(String msg) -> {System.out.println(msg);}
has everything.
Then we can omit the parameter type to have
(msg)->{System.out.println(msg);}
We can further omit the parameter type and parentheses as follows.
msg -> { System.out.println(msg); }
Example
public class Main {
public static void main(String[] argv) {
Processor stringProcessor = str -> str.length();
String name = "Java Lambda";
int length = stringProcessor.getStringLength(name);
System.out.println(length);/*from w w w. j a v a 2s . c om*/
}
}
@FunctionalInterface
interface Processor {
int getStringLength(String str);
}
The code above generates the following result.