
本文通过几个简单的例子演示 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.6Reciprocal of 4 is 0.25Reciprocal of 8 is 0.125
示例三:NumericTest
AppMall应用商店
AI应用商店,提供即时交付、按需付费的人工智能应用服务
56 查看详情
此示例展示了如何使用 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 (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 103 is not a factor of 102 is less than 1010 is not less than 2Absolute 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 表达式的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/612162.html
微信扫一扫
支付宝扫一扫