在Java中阻止 Lambda 表达式
Lambda 表达式是一种未自行执行的未命名方法。这些表达式导致匿名类。这些 lambda 表达式称为闭包。 Lambda 的主体由一段代码组成。如果它只有一个表达式,则称为“表达式体”。包含表达式主体的 Lambda 被称为“表达式 Lambda”。下面是一行中的 Lambda 表达式示例。
Block Lambda 包含许多适用于 lambda 表达式的操作,因为它允许 lambda 主体具有许多语句。这包括变量、循环、条件语句(如 if、else 和 switch 语句)、嵌套块等。这是通过将 lambda 主体中的语句块括在大括号 {} 中来创建的。这甚至可以有一个返回语句,即返回值。
语法: Lambda 表达式
(parameters) -> { lambda body }
现在首先让我们了解 Lambda 表达式以了解块 lambda 表达式。
插图:
在这种情况下,lambda 在其 lambda 主体中可能需要多个表达式。 Java支持表达式 Lambda,它包含具有多个语句的代码块。我们称这种类型的 Lambda Body 为“块体”。包含块体的 Lambda 可以称为“ Block Lambda s ”。
表示 Lambda 表达式的示例
Java
// Java Program to illustrate Lambda expression
// Importing input output classes
import java.io.*;
// Interface
// If1 is name of this interface
interface If1 {
// Member function of this interface
// Abstract function
boolean fun(int n);
}
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Lambda expression body
If1 isEven = (n) -> (n % 2) == 0;
// Above is lambda expression which tests
// passed number is even or odd
// Condition check over some number N
// by calling the above function
// using isEven() over fun() defined above
// Input is passed as a parameter N
// Say custom input N = 21
if (isEven.fun(21))
// Display message to be printed
System.out.println("21 is even");
else
// Display message to be printed
System.out.println("21 is odd");
}
}
Java
// Java Program to illustrate Block Lambda expression
// Importing all classes from
// java.util package
import java.io.*;
// Block lambda to find out factorial
// of a number
// Interface
interface Func {
// n is some natural number whose
// factorial is to be computed
int fact(int n);
}
// Class
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Block lambda expression
Func f = (n) ->
{
// Block body
// Initially initializing with 1
int res = 1;
// iterating from 1 to the current number
// to find factorial by multiplication
for (int i = 1; i <= n; i++)
res = i * res;
return res;
};
// Calling lambda function
// Print and display n the console
System.out.println("Factorial of 5 : " + f.fact(5));
}
}
Java
// Java Program to illustrate Block Lambda expression
// Importing all input output classes
import java.io.*;
// Interface
// Functional interface named 'New'
interface New {
// Boolean function to check over
// natural number depicting calender year
// 'n' deepicting year is
// passed as an parameter
boolean test(int n);
}
// Class
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// block lambda
// This block lambda checks if the
// given year is leap year or not
New leapyr = (year) ->
{
// Condition check
// If year is divisible by 400 or the
// year is divisible by 4 and 100 both
if (((year % 400 == 0)
|| (year % 4 == 0) && (year % 100 != 0)))
// Returning true as year is leap year
return true;
else
// Returning false for non-leap years
return false;
};
// Calling lambda function over
// custom input year- 2020
// Condition check using the test()
// defined in the above interface
if (leapyr.test(2020))
// Display message on the console
System.out.println("leap year");
else
// Display message on the console
System.out.println("Non leap year");
}
}
21 is odd
现在切换到执行 块 lambda 表达式后跟两个示例,如下所示:
执行:
示例 1:
Java
// Java Program to illustrate Block Lambda expression
// Importing all classes from
// java.util package
import java.io.*;
// Block lambda to find out factorial
// of a number
// Interface
interface Func {
// n is some natural number whose
// factorial is to be computed
int fact(int n);
}
// Class
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Block lambda expression
Func f = (n) ->
{
// Block body
// Initially initializing with 1
int res = 1;
// iterating from 1 to the current number
// to find factorial by multiplication
for (int i = 1; i <= n; i++)
res = i * res;
return res;
};
// Calling lambda function
// Print and display n the console
System.out.println("Factorial of 5 : " + f.fact(5));
}
}
Factorial of 5: 120
Here in this block lambda declares a variable ‘res’, for loop and has return statement which are legal in lambda body.
示例 2:
Java
// Java Program to illustrate Block Lambda expression
// Importing all input output classes
import java.io.*;
// Interface
// Functional interface named 'New'
interface New {
// Boolean function to check over
// natural number depicting calender year
// 'n' deepicting year is
// passed as an parameter
boolean test(int n);
}
// Class
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// block lambda
// This block lambda checks if the
// given year is leap year or not
New leapyr = (year) ->
{
// Condition check
// If year is divisible by 400 or the
// year is divisible by 4 and 100 both
if (((year % 400 == 0)
|| (year % 4 == 0) && (year % 100 != 0)))
// Returning true as year is leap year
return true;
else
// Returning false for non-leap years
return false;
};
// Calling lambda function over
// custom input year- 2020
// Condition check using the test()
// defined in the above interface
if (leapyr.test(2020))
// Display message on the console
System.out.println("leap year");
else
// Display message on the console
System.out.println("Non leap year");
}
}
leap year
Here in this block lambda has if-else conditions and return statements which are legal in lambda body.