本文通过几个简单的例子演示 Lambda 表达式的基本用法。
示例一:传统方法与 Lambda 表达式对比
传统方法(不使用 Lambda):
interface MyValueSemLambda1 { double getValue(); } class MyValueImpl implements MyValueSemLambda1 { private double value; public MyValueImpl(double value) { this.value = value; } @Override public double getValue() { return this.value; } } public class MyValueSemLambda { public static void main(String[] args) { MyValueSemLambda1 myVal = new MyValueImpl(98.6); System.out.println("Value: " + myVal.getValue()); // Prints 98.6 } }
Lambda 表达式方法:
interface MyValueCompare { double getValue(); } public class MyValueComparacao { public static void main(String[] args) { MyValueCompare myVal = () -> 98.6; System.out.println("Value: " + myVal.getValue()); // Prints 98.6 } }
示例二:LambdaDemo
此示例展示了如何使用 Lambda 表达式实现不同的函数式接口,包括常量表达式和带参数的表达式。
interface MyValue { double getValue(); } interface MyParamValue { double getValue(double v); } class LambdaDemo { public static void main(String args[]) { MyValue myVal; myVal = () -> 98.6; System.out.println("Constant value: " + myVal.getValue()); MyParamValue myPval = (n) -> 1.0 / n; System.out.println("Reciprocal of 4 is " + myPval.getValue(4.0)); System.out.println("Reciprocal of 8 is " + myPval.getValue(8.0)); } }
输出:
Constant value: 98.6 Reciprocal of 4 is 0.25 Reciprocal of 8 is 0.125
示例三:NumericTest
此示例展示了如何使用 Lambda 表达式创建不同的测试,例如整除性测试、大小比较和绝对值比较。
interface NumericTest { boolean test(int n, int m); } class LambdaDemo2 { public static void main(String args[]) { NumericTest isFactor = (n, d) -> (n % d) == 0; if (isFactor.test(10, 2)) System.out.println("2 is a factor of 10"); if (!isFactor.test(10, 3)) System.out.println("3 is not a factor of 10"); NumericTest lessThan = (n, m) -> (n < m); if (lessThan.test(2, 10)) System.out.println("2 is less than 10"); if (!lessThan.test(10, 2)) System.out.println("10 is not less than 2"); NumericTest absEqual = (n, m) -> (Math.abs(n) == Math.abs(m)); if (absEqual.test(4, -4)) System.out.println("Absolute values of 4 and -4 are equal."); if (!absEqual.test(4, -5)) System.out.println("Absolute values of 4 and -5 are not equal."); } }
输出:
2 is a factor of 10 3 is not a factor of 10 2 is less than 10 10 is not less than 2 Absolute values of 4 and -4 are equal. Absolute values of 4 and -5 are not equal.
示例四:字符串测试
这个例子展示了如何使用 Lambda 表达式来测试字符串条件。
interface StringTest { boolean test(String aStr, String bStr); } class LambdaDemo3 { public static void main(String args[]) { StringTest isIn = (a, b) -> a.indexOf(b) != -1; String str = "This is a test"; System.out.println("Test string: " + str); if (isIn.test(str, "is a")) System.out.println("'is a' found."); else System.out.println("'is a' not found."); if (isIn.test(str, "xyz")) System.out.println("'xyz' found"); else System.out.println("'xyz' not found"); } }
输出:
Test string: This is a test 'is a' found. 'xyz' not found
这些示例展示了 Lambda 表达式在 Java 中的灵活性和简洁性。 记住,Lambda 表达式必须与函数式接口的抽象方法兼容。 使用清晰的变量名可以提高代码的可读性。
以上就是实际应用中的 Lambda 表达式的详细内容,更多请关注知识资源分享宝库其它相关文章!
版权声明
本站内容来源于互联网搬运,
仅限用于小范围内传播学习,请在下载后24小时内删除,
如果有侵权内容、不妥之处,请第一时间联系我们删除。敬请谅解!
E-mail:dpw1001@163.com
发表评论