[Java 8] Lambda 表达式实例
- - Java - 编程语言 - ITeye博客Java 8 中的 Lambda 表达式,允许将函数作为形参传递给另外的函数. 为了更好地理解,我们用实例的方式来演示如何使用 Lambda 表达式. 1、Lambda 表达式 Hello World. 这是一个最简单的 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")); } }
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(); } }
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); } }
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()); } }
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; } }
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)); } }