OnJava8-Examples/functions/LambdaExpressions.java

48 lines
1016 B
Java
Raw Normal View History

2015-11-03 12:00:44 -08:00
// functions/LambdaExpressions.java
2015-06-15 17:47:35 -07:00
interface Description {
String brief();
}
interface Body {
String detailed(String head);
}
interface Multi {
String twoArg(String head, Double d);
}
public class LambdaExpressions {
Description desc = new Description() {
@Override
public String brief() {
return "Short info";
}
};
Description desc2 = () -> "Short info";
Body bod = (h) -> h + " More details";
Body bod2 = h -> h + " No Parens!";
Multi mult = (h, n) -> h + n;
// Parens are required with multiple args:
// Multi mult2 = h, n -> h + n; // Nope
public static void main(String[] args) {
LambdaExpressions le =
new LambdaExpressions();
2015-11-03 12:00:44 -08:00
System.out.println(le.desc.brief());
System.out.println(le.desc2.brief());
System.out.println(le.bod.detailed("Hi!"));
System.out.println(le.bod2.detailed("Oh!"));
System.out.println(le.mult.twoArg("Pi! ", 3.14159));
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Short info
Short info
Hi! More details
Oh! No Parens!
Pi! 3.14159
2015-09-07 11:44:36 -06:00
*/