在这里,我们可以将一个匿名类传递给一个方法。这有助于在Java7中编写代码更少的程序。但是,语法仍然很难,需要很多额外的代码行。

Java8进一步扩展了SAMs的功能。因为我们知道函数接口只有一个方法,所以在将其作为参数传递时,不需要定义该方法的名称。Lambda表达式允许我们完全做到这一点。

##Lambda表达式简介

Lambda表达式本质上是一个匿名或未命名的方法。lambda表达式不会自行执行。相反,它用于实现由函数接口定义的方法。

如何在Java中定义lambda表达式?

下面是如何在Java中定义lambda表达式。

(parameter list) -> lambda body

使用的新运算符(->)称为箭头运算符或lambda运算符。语法目前可能不清楚。让我们来探讨一些例子,

假设我们有这样一种方法:

double getPiValue() {
return 3.1415;
}

我们可以使用lambda表达式编写此方法,如下所示:

() -> 3.1415

这里,该方法没有任何参数。因此,运算符的左侧包含一个空参数。右侧是lambda主体,指定lambda表达式的操作。在本例中,它返回值3.1415。

Lambda体的类型

在Java中,lambda主体有两种类型。

  1. 具有单个表达式的主体
() -> System.out.println(“Lambdas are great”);

这种类型的lambda体称为表达式体。

  1. 由代码块组成的主体。
() -> {
double pi = 3.1415;
return pi;
};

这种类型的lambda体称为块体。块体允许lambda体包含多个语句。这些语句包含在大括号内,必须在大括号后添加分号。

注意:对于块体,如果块体返回值,则可以使用return语句。但是,表达式体不需要返回语句。

示例3:Lambda表达式

让我们编写一个Java程序,使用lambda表达式返回Pi的值。

如前所述,lambda表达式不会单独执行。相反,它形成了由函数接口定义的抽象方法的实现。

因此,我们需要首先定义一个功能接口。

import java.lang.FunctionalInterface;
// this is functional interface
@FunctionalInterface
interface MyInterface{
// abstract method
double getPiValue();
}
public class Main {
public static void main( String[] args ) {
// declare a reference to MyInterface
MyInterface ref;
// lambda expression
ref = () -> 3.1415;
System.out.println("Value of Pi = " + ref.getPiValue());
}
}
输出:
Value of Pi = 3.1415

在上面的例子中,

我们已经创建了一个名为MyInterface的功能接口。它包含一个名为getPiValue()的抽象方法

在主类中,我们声明了对MyInterface的引用。注意,我们可以声明接口的引用,但不能实例化接口。就是,

// it will throw an error
MyInterface ref = new myInterface();
// it is valid
MyInterface ref;

然后,我们为引用指定了一个lambda表达式。

ref = () -> 3.1415;

最后,我们使用引用接口调用方法getPiValue()

System.out.println("Value of Pi = " + ref.getPiValue());

带参数的Lambda表达式

到目前为止,我们已经创建了没有任何参数的lambda表达式。但是,与方法类似,lambda表达式也可以有参数。

(n) -> (n%2)==0

这里,括号内的变量n是传递给lambda表达式的参数。lambda主体接受参数并检查它是偶数还是奇数。

示例4:使用带参数的lambda表达式

@FunctionalInterface
interface MyInterface {
// abstract method
String reverse(String n);
}
public class Main {
public static void main( String[] args ) {
// declare a reference to MyInterface
// assign a lambda expression to the reference
MyInterface ref = (str) -> {
String result = “”;
for (int i = str.length()-1; i >= 0 ; i–)
result += str.charAt(i);
return result;
};
// call the method of the interface
System.out.println("Lambda reversed = " + ref.reverse(“Lambda”));
}
}
输出:
Lambda reversed = adbmaL
##通用功能接口

到目前为止,我们使用的函数接口只接受一种类型的值。例如

@FunctionalInterface
interface MyInterface {
String reverseString(String n);
}

上面的函数接口只接受字符串并返回字符串。但是,我们可以使函数接口通用,以便接受任何数据类型。如果您不确定泛型,请访问Java泛型。

示例5:通用函数接口和Lambda表达式

// GenericInterface.java
@FunctionalInterface
interface GenericInterface {
// generic method
T func(T t);
}
// GenericLambda.java
public class Main {
public static void main( String[] args ) {
// declare a reference to GenericInterface
// the GenericInterface operates on String data
// assign a lambda expression to it
GenericInterface reverse = (str) -> {
String result = “”;
for (int i = str.length()-1; i >= 0 ; i–)
result += str.charAt(i);
return result;
};
System.out.println("Lambda reversed = " + reverse.func(“Lambda”));
// declare another reference to GenericInterface
// the GenericInterface operates on Integer data
// assign a lambda expression to it
GenericInterface factorial = (n) -> {
int result = 1;
for (int i = 1; i <= n; i++)
result = i * result;
return result;
};
System.out.println("factorial of 5 = " + factorial.func(5));
}
}
输出:
Lambda reversed = adbmaL
factorial of 5 = 120

在上面的示例中,我们创建了一个名为GenericInterface的通用函数接口。它包含一个名为func()的泛型方法。

在这里,在主类中,

  • GenericInterface<String>reverse-创建对接口的引用。该接口现在对字符串类型的数据进行操作。
  • GenericInterface<Integer>factorial-创建对接口的引用。在本例中,接口对整型数据进行操作。

Lambda表达式和StreamAPI

新的java.util.stream包已添加到JDK8中,它允许java开发人员执行搜索、筛选、映射、减少或操作列表等集合。

例如,我们有一个数据流(在本例中是一个字符串列表),其中每个字符串都是国家名称和国家地点的组合。现在,我们可以处理这些数据流,只从尼泊尔检索地方。