首页 > Java > java教程 > 正文

实际应用中的 Lambda 表达式

霞舞
发布: 2025-01-07 15:07:51
原创
672人浏览过

实际应用中的 lambda 表达式

本文通过几个简单的例子演示 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 表达式的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号