Java 8 中的 Lambda 表达式,允许将函数作为形参传递给另外的函数。为了更好地理解,我们用实例的方式来演示如何使用 Lambda 表达式。
[b][size=x-large]1、Lambda 表达式 Hello World[/size][/b]
这是一个最简单的 Lambda 表达式的例子。首先在 main 方法的上面声明了一个接口 HelloWorld,在 main 方法中实现了这个接口,随后调用了接口的唯一方法。
[b][size=x-large]2、在 Lambda 表达式中访问局部变量和成员变量[/size][/b]
在这个例子中演示了如何在 Lambda 表达式中访问局部变量和成员变量,请注意 Runnable 接口的用法。
[b][size=x-large]3、Lambda 表达式中的方法传递[/size][/b]
这个例子比前面的更高级一些。对与 Circle 接口,有两个不同的实现。这些实现本身作为参数传递到了另外一个方法中。
[b][size=x-large]4、Lambda 表达式的初始化[/size][/b]
[b][size=x-large]5、Lambda 表达式进行排序[/size][/b]
这个例子的关键是将方法的引用传递给了另一个方法来进行调用。Comparator 接口的具体实现作为参数传递给了 Arrays.sort 方法。
[b][size=x-large]6、条件判断(Predicates) 和 Lambda 表达式[/size][/b]
条件判断和 Lambda 之间结合的非常好。我们可以使用 Lambda 来实现 Predicate 接口,并且将其传递给具体的方法,作为方法的判断条件。
文章来源:[url]http://www.aptusource.org/2014/03/java-8-lambda-example/[/url]
[b][size=x-large]1、Lambda 表达式 Hello World[/size][/b]
这是一个最简单的 Lambda 表达式的例子。首先在 main 方法的上面声明了一个接口 HelloWorld,在 main 方法中实现了这个接口,随后调用了接口的唯一方法。
public class LambdaHelloWorld {
interface HelloWorld {
String hello(String name);
}
public static void main(String[] args) {
HelloWorld helloWorld = (String name) -> { return "Hello " + name; };
System.out.println(helloWorld.hello("Joe"));
}
}
[b][size=x-large]2、在 Lambda 表达式中访问局部变量和成员变量[/size][/b]
在这个例子中演示了如何在 Lambda 表达式中访问局部变量和成员变量,请注意 Runnable 接口的用法。
public class LambdaVariableAccess {
public String wildAnimal = "Lion";
public static void main(String[] arg) {
new LambdaVariableAccess().lambdaExpression();
}
public void lambdaExpression(){
String domesticAnimal = "Dog";
new Thread (() -> {
System.out.println("Class Level: " + this.wildAnimal);
System.out.println("Method Level: " + domesticAnimal);
}).start();
}
}
[b][size=x-large]3、Lambda 表达式中的方法传递[/size][/b]
这个例子比前面的更高级一些。对与 Circle 接口,有两个不同的实现。这些实现本身作为参数传递到了另外一个方法中。
public class LambdaFunctionArgument {
interface Circle {
double get(double radius);
}
public double circleOperation(double radius, Circle c) {
return c.get(radius);
}
public static void main(String args[]){
LambdaFunctionArgument reference = new LambdaFunctionArgument();
Circle circleArea = (r) -> Math.PI * r * r;
Circle circleCircumference = (r) -> 2 * Math.PI * r;
double area = reference.circleOperation(10, circleArea);
double circumference = reference.circleOperation(10, circleCircumference);
System.out.println("Area: "+area+" . Circumference: "+circumference);
}
}
[b][size=x-large]4、Lambda 表达式的初始化[/size][/b]
import java.util.concurrent.Callable;
public class LambdaInitialization {
public static void main(String args[]) throws Exception{
Callable[] animals = new Callable[]{()->"Lion", ()->"Crocodile"};
System.out.println(animals[0].call());
}
}
[b][size=x-large]5、Lambda 表达式进行排序[/size][/b]
这个例子的关键是将方法的引用传递给了另一个方法来进行调用。Comparator 接口的具体实现作为参数传递给了 Arrays.sort 方法。
import java.util.Arrays;
public class LambdaExpressionSort {
public static void main (String[] ar){
Animal[] animalArr = {
new Animal("Lion"),
new Animal("Crocodile"),
new Animal("Tiger"),
new Animal("Elephant")};
System.out.println("Before Sort: "+Arrays.toString(animalArr));
Arrays.sort(animalArr, Animal::animalCompare);
System.out.println("After Sort: "+Arrays.toString(animalArr));
}
}
class Animal {
String name;
Animal(String name) {
this.name = name;
}
public static int animalCompare(Animal a1, Animal a2) {
return a1.name.compareTo(a2.name);
}
public String toString() {
return name;
}
}
[b][size=x-large]6、条件判断(Predicates) 和 Lambda 表达式[/size][/b]
条件判断和 Lambda 之间结合的非常好。我们可以使用 Lambda 来实现 Predicate 接口,并且将其传递给具体的方法,作为方法的判断条件。
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class LambdaPredicate {
public static int add(List numList, Predicate predicate) {
int sum = 0;
for (int number : numList) {
if (predicate.test(number)) {
sum += number;
}
}
return sum;
}
public static void main(String args[]){
List numList = new ArrayList();
numList.add(new Integer(10));
numList.add(new Integer(20));
numList.add(new Integer(30));
numList.add(new Integer(40));
numList.add(new Integer(50));
System.out.println("Add Everything: "+add(numList, n -> true));
System.out.println("Add Nothing: "+add(numList, n -> false));
System.out.println("Add Less Than 25: "+add(numList, n -> n < 25));
System.out.println("Add 3 Multiples: "+add(numList, n -> n % 3 == 0));
}
}
文章来源:[url]http://www.aptusource.org/2014/03/java-8-lambda-example/[/url]