Introduction
Lambda expression is used to replace one method classes/interface with inline code.
Syntax
(arg1, arg2...) -> { body }
- Declaring the types of the parameters is optional.
- Using parentheses around the parameter is optional if you have only one parameter.
- Using curly braces is optional (unless you need multiple statements).
- The “return” keyword is optional if you have a single expression that returns a value.
Examples
new Thread(() -> {
System.out.println("Hello from runnable.");
}).start();
List<String> s = Arrays.asList(new String[] { "foo", "bar", "code" });
Collections.sort(s, (String s1, String s2) -> {
return s1.compareTo(s2);
});
Collections.sort(s, (s1, s2) -> s1.compareTo(s2));
for (String i : s) {
System.out.println(i);
}
Reference
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html
No comments:
Post a Comment