public class Java8Tester {public static void main(String args[]) {Java8Tester tester = new Java8Tester();
编程学习网为您整理以下代码实例,主要实现:Java Lambda表达式示例,希望可以帮到各位朋友。
public class Java8Tester {
public static voID main(String args[]) {
Java8Tester tester = new Java8Tester();
//with type declaration
MathOperation addition = (int a, int b) -> a + b;
//with out type declaration
MathOperation subtraction = (a, b) -> a - b;
//with return statement along with curly braces
MathOperation multiplication = (int a, int b) -> { return a * b; };
//without return statement and without curly braces
MathOperation division = (int a, int b) -> a / b;
System.out.println("10 + 5 = " + tester.operate(10, 5, addition));
System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction));
System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication));
System.out.println("10 / 5 = " + tester.operate(10, 5, division));
//without parenthesis
GreetingService greetService1 = message ->
System.out.println("Hello " + message);
//with parenthesis
GreetingService greetService2 = (message) ->
System.out.println("Hello " + message);
greetService1.sayMessage("Mahesh");
greetService2.sayMessage("Suresh");
}
interface MathOperation {
int operation(int a, int b);
}
interface GreetingService {
voID sayMessage(String message);
}
private int operate(int a, int b, MathOperation mathOperation) {
return mathOperation.operation(a, b);
}
}
沃梦达教程
本文标题为:Java Lambda表达式示例
基础教程推荐
猜你喜欢
- 整数类型 1970-01-01
- Java.io.ObjectStreamField.isUnshared()方法 1970-01-01
- Java方法引用示例 1970-01-01
- java.util.zip.Inflater.read()方法 1970-01-01
- Java Lambda表达式示例 1970-01-01
- Java.io.ObjectStreamField.toString()方法 1970-01-01
- JAVA通过正则匹配html里面body标签的内容 2022-10-25
- Hello World示例 1970-01-01
- Java8示例程序 1970-01-01