
本文通过几个简单的例子演示 Lambda 表达式的基本用法。
示例一:传统方法与 Lambda 表达式对比
传统方法(不使用 Lambda):
<code class="java">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
}
}</code>Lambda 表达式方法:
<code class="java">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
}
}</code>示例二:LambdaDemo
此示例展示了如何使用 Lambda 表达式实现不同的函数式接口,包括常量表达式和带参数的表达式。
<code class="java">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));
}
}</code>输出:
<code>Constant value: 98.6 Reciprocal of 4 is 0.25 Reciprocal of 8 is 0.125</code>
示例三:NumericTest
此示例展示了如何使用 Lambda 表达式创建不同的测试,例如整除性测试、大小比较和绝对值比较。
<code class="java">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.");
}
}</code>输出:
<code>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.</code>
示例四:字符串测试
这个例子展示了如何使用 Lambda 表达式来测试字符串条件。
<code class="java">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");
}
}</code>输出:
<code>Test string: This is a test 'is a' found. 'xyz' not found</code>
这些示例展示了 Lambda 表达式在 Java 中的灵活性和简洁性。 记住,Lambda 表达式必须与函数式接口的抽象方法兼容。 使用清晰的变量名可以提高代码的可读性。
以上就是实际应用中的 Lambda 表达式的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号