Java lambda表达式详解
Java lambda表达式是一种很有用很方便很常见的语法,先据一个例子:
public class Java8Tester { public static void main(String args[]){ Java8Tester tester = new Java8Tester(); //with type declaration MathOperation addition = (int a, int b) -> a + b; //with out type declaration MathOperation subtraction = (a, b) -> a - b; //with return statement along with curly braces MathOperation multiplication = (int a, int b) -> { return a * b; }; //without return statement and without curly braces MathOperation division = (int a, int b) -> a / b; System.out.println("10 + 5 = " + tester.operate(10, 5, addition)); System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction)); System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication)); System.out.println("10 / 5 = " + tester.operate(10, 5, division)); //without parenthesis GreetingService greetService1 = message -> System.out.println("Hello " + message); //with parenthesis GreetingService greetService2 = (message) -> System.out.println("Hello " + message); greetService1.sayMessage("Mahesh"); greetService2.sayMessage("Suresh"); } interface MathOperation { int operation(int a, int b); } interface GreetingService { void sayMessage(String message); } private int operate(int a, int b, MathOperation mathOperation){ return mathOperation.operation(a, b); } }
可以看出Java lambda表达式的形式为 parameter -> expression。上面程序中如果不用lambda表达式,也可以实现,比如用类实现接口,然后生成对象,再调用之类的,或者好一点,也是用一个匿名类,但是这样会非常麻烦,远远没有lambda表达式简洁.
再看另一个例子。如果有一个Person列表,Person包含年龄,性别,名字等字段,现在要写一个根据条件过滤这个列表并打印出来等程序,用lambda表达式可以这么做:
import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; import java.util.function.Predicate; class Person { public enum Sex { MALE, FEMALE } String name; Sex gender; int age; String emailAddress; public int getAge() { return age; } public Sex getGender() { return gender; } public void setGender(Sex gender) { this.gender = gender; } public void printPerson() { System.out.println( "The name is: "+ name + " at age: "+ age); } public Person(String name, Sex gender, int age, String emailAddress) { this.name = name; this.gender = gender; this.age = age; this.emailAddress = emailAddress; } } public class Main { public static void printPersonsWithPredicate(List<Person> roster, Predicate<Person> tester) { for (Person p : roster) { if (tester.test(p)) { p.printPerson(); } } } public static void processPersons( List<Person> roster, Predicate<Person> tester, Consumer<Person> block) { for (Person p : roster) { if (tester.test(p)) { block.accept(p); } } } public static void main(String[] args){ List<Person> pl = new ArrayList(); pl.add(new Person("Jet", Person.Sex.MALE, 21, "Jet@163.com")); pl.add(new Person("Mike", Person.Sex.MALE, 18, "Mike@163.com")); pl.add(new Person("Jim", Person.Sex.MALE, 28, "Jim@163.com")); pl.add(new Person("Lucy", Person.Sex.FEMALE, 20, "Lucy@163.com")); pl.add(new Person("Diana", Person.Sex.FEMALE, 28, "Diana@163.com")); printPersonsWithPredicate( pl, p -> p.getGender() == Person.Sex.FEMALE && p.getAge() >= 18 && p.getAge() <= 25 ); System.out.println("=========================="); processPersons( pl, p -> p.getGender() == Person.Sex.MALE && p.getAge() >= 18 && p.getAge() <= 25, p -> p.printPerson() ); } }
可以看到这里分别调用了Java的标准接口Predicate和Consumer,返回类型不同需要注意,然后我们用lambda表达式逻辑上实现它的test方法和accept,就可以正确使用了,是不是很方便?