首頁  >  文章  >  Java  >  Java Lambda 表達式

Java Lambda 表達式

WBOY
WBOY原創
2024-08-30 16:16:201004瀏覽

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn