首页 > Java > java教程 > 正文

Java Lambda 表达式

WBOY
发布: 2024-08-30 16:16:20
原创
955 人浏览过

Lambda 表达式是 Java8 中添加和引入的新功能。它基本上描述了功能接口的所有实例。它的实现方式是仅包含一个抽象函数并实现函数接口。 Java 8 Lambda 表达式可以创建方法参数或将整个代码视为数据。它可以实现没有任何特定类的功能,这意味着抽象类。该表达式可以传递给下一个对象,并且可以在需要时执行。 Lambda 表达式可以被视为与函数一样,并且可以像其他函数一样接受参数。

语法:

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

lambda_operator -> body
登录后复制

Java Lambda 表达式的参数

传递给 lambda_operator 的参数如下:

  • # 零参数:这个参数意味着它不能传递函数中的任何参数,如下所示:
  • (): System.out.println(“Zerolambda 参数”);
  • # 一个参数:该参数表示可以传递函数中的任意一个参数,如下所示:
  • (p): System.out.println(“一个参数:” + p);
  • # 多参数:顾名思义,该参数可以用来向操作符传递多个参数,其表示如下:
  • (q1, q2): System.out.println(“多个参数:” + q1 + “, ” + q2);

为什么我们需要 Java Lambda 表达式?

Lambda 表达式随 Java 8 一起出现,并转换和简化了可以传递执行的代码块的所有复杂执行。这是许多其他编程语言都使用的一个非常常见的功能,Java 8 也是如此;在引入之后,java的早期特性首先需要创建一个对象,然后传递该对象,例如制定策略化的设计模式。但 lambda 表达式成为了上述问题的救世主,因为它是增强函数式接口实现的附加值。它通过指向程序主体的 lambda_operator 函数简化了实现,该函数拥有零个、一个和多个要传递给函数的参数。它还利用了创建的且不属于任何已定义类的函数。它有一个非常好的特性,可以采用功能作为函数的方法参数,将其视为一个整体的数据。

Java 8 中的 Lambda 表达式非常强大且非常引人注目。强烈建议将与功能接口相关的对象转换为最终输出。

Java Lambda 表达式如何工作?

任何表达式都存在隐藏的工作模式,因此 Lambda 表达式也具有一些工作模式,如下所示:

(int arg_a, String arg_b)
{System.out.println("two arguments"+ arg_a+" and "+arg_b);}
登录后复制

这两个参数 int arg_a 和 String arg_b 构成了具有零个参数、一个参数或两个以上参数(即多个参数)的参数列表。这些参数作为参数列表在箭头标记的帮助下传递到 lambda 表达式的主体。该箭头标记及其参数列表连续跟随 lambda 主体。此外,这种格式的 lambda 表达式进一步利用函数接口来实现。但如果是多个参数列表,则必须关闭括号或代码块,然后匿名函数的返回类型将与需要返回代码块或 void if 的值类型相同未归还或不具有任何价值。

Java Lambda 表达式的实现示例

以下是 Java Lambda 表达式的示例:

示例#1

该程序说明了不使用 lambda 表达式的接口执行,甚至没有创建用于打印矩形大小的抽象类。

代码:

interface Shapes
{
public void rectangle();
}
public class WithoutLambdaExpression {
public static void main(String[] args) {
int size=15;
Shapes wd=new Shapes(){
public void rectangle()
{
System.out.println("Get the shape" +  size );
}
};
wd.rectangle();
}
}
登录后复制

输出:

Java Lambda 表达式

示例#2

该程序演示了使用 lambda 表达式创建功能接口,从而提供形状所需的输出。

代码:

interface Shapes{
public void Rectangle();
}
public class UsinglmbdaExpression {
public static void main(String[] args) {
int size=20;
Shapes r8=()->
{
System.out.println("Shapes of all sizes "+ size);
};
r8.Rectangle();
}
}
登录后复制

输出:

Java Lambda 表达式

Example #3

This program is used to illustrate the Lambda Expression for Java 8 by not passing any parameter implemented just with the function parameter and the associated abstract classes of the implemented functional interface as shown.

Code:

interface SomethingFishy{
public String strange();
}
public class LambdaWithNoArg{
public static void main(String[] args) {
SomethingFishy k=()->{
return "Very Strange View.";
};
System.out.println(k.strange());
}
}
登录后复制

Output:

Java Lambda 表达式

Example #4

This program illustrates the lambda Expression with the implementation of functional parameters, thereby passing the single parameter from the function of the interface and the lambda expression associated with it.

Code:

interface JuicyFruit{
public String juicy(String name);
}
public class SinglParamLambda
{
public static void main(String[] args)
{
JuicyFruit jf1=(name)->{
return "Kiwi, "+name;
};
System.out.println(jf1.juicy("orange"));
JuicyFruit jf2= name ->{
return "Mango, "+name;
};
System.out.println(jf2.juicy("Pineapple"));
}
}
登录后复制

Output:

Java Lambda 表达式

Example #5

This program is used to illustrate the Multiple parameter lambda Expression with the functional interface of division and its values to be divided using the class’s div function.

Code:

interface Division{
int div(int p,int q);
}
public class MultiParamLambdaExpression{
public static void main(String[] args) {
Division div1=(p,q)->(p/q);
System.out.println(div1.div(20,40));
Division div2=(int p,int q)->(p/q);
System.out.println(div2.div(400,600));
}
}
登录后复制

Output

Java Lambda 表达式

Note: Lambda expressions are exclusively used to implement the functional interfaces, and they can contain any type of argument such as zero, two or multiple.

Conclusion

Lambda Expression introduced with Java 8 has simplified the execution of the block of code using the array operator and operator arrow pointing to the lambda body, and then it will be used for implementing the interfaces with the abstract classes associated with each class. Thus, Lambda Expression of Java 8 has really transformed the interface interaction and the implementation of the functions and method easier with the abstract classes.

以上是Java Lambda 表达式的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!