在《挑苹果中的行为参数化思想》已经介绍了使用Lambda表达式将行为抽象化,对Lambda表达式有必定认识。而本文将对Lambda表达式进行系统性的介绍。java
首先咱们要知道如何写Lambda表达式,或者说怎么样才能写出有效的Lambda表达式,这就须要了解其语法。express
Lambda表达式由三部分组成:segmentfault
有两种风格,分别是:app
(parameters) -> expression
函数
(parameters) -> { statements; }
工具
依据上面的风格介绍,来试着判断下面给出的示例是否有效:ui
() -> {}
() -> "Apple"
() -> { return "Apple"; }
(Integer i) -> return "Apple" + i
(String s) -> { "Apple"; }
解析:(1)是块风格,没有语句;(2)是表达式风格,一个字符串表达式;(3)是块风格,有花括号和返回语句;(4)非有效,写了返回语句,但缺乏花括号,补上花括号和分号,为块风格,而去掉return
则为表达式风格;(5)非有效,"Apple"
是一个字符串表达式,不是一个语句,加上return
,或者去掉分号和花括号。this
Lambda表达式写好了,咱们要知道哪里能用Lambda表达式。已知Lambda表达式可看做是匿名内部类的实现,那对于匿名内部类来讲最重要的是类所实现的接口,而Lambda表达式是否可用于全部接口?答案“不是的”,Lambda表达式对接口有必定的要求,必须是函数式接口。spa
所谓的函数式接口指的是只定义一个抽象方法的接口。code
例如:
public interface Comparator<T> { int compare(T o1, T o2); }
public interface Runnable { void run(); }
public interface Callable<V> { V call() throws Exception; }
可见上面三个接口都只有一个抽象方法,可是三个方法的签名都不同,这要求Lambda表达式与实现接口的方法签名要一致。下面用函数描述符来表示上述三个方法的签名,箭头前面是方法的入参类型,后面是返回类型。
(T, T) -> int
,两个泛型T类型的入参,返回int类型Lambda表达式:(Apple a1, Apple a2) -> a1.getWeight - a2.getWeight
() -> void
,无入参,无返回值Lambda表达式:() -> { System.out.println("Hi"); }
() -> V
,无入参,返回一个泛型V类型的对象Lambda表达式:() -> new Apple()
看call方法的示例,你是否会疑惑,new Apple()
是一个语句,为何没有花括号和分号,是否是非有效的。你须要记住这是合法的,这是一个特殊的规定,不须要用括号环绕返回值为void的单行方法调用。
下面介绍在Java中内置的经常使用Lambda表达式:
public interface Predicate<T> { boolean test(T t); }
test:T -> boolean
,接收一个泛型T对象,返回一个boolean。
适用场景:表示一个涉及类型T的布尔表达式。
// 判断空白字符串 Predicate<String> blankStrPredicate = s -> s != null && s.trim().length() == 0; blankStrPredicate.test(" "); // true // 判断苹果重量是否大于150 Predicate<Apple> heavyApplePredicate = a -> a.getWeight() > 150; heavyApplePredicate.test(new Apple(100)); // false
注意,参数部分缺乏了参数类型,是由于可根据上下文推断出Lambda表达式的参数类型,因此能够省略不写。好比这里由于将Lambda表达式赋值给一个Predicate<String>类型的变量,又由于函数描述符为
(T) -> boolean
,则可推断出参数T的实际类型为String。并且当只有一个参数时,能够将括号也省略。
public interface Consumer<T> { void accept(T t); }
accept:T -> void
,接收一个泛型T对象,无返回值(void)。
适用场景:访问类型T的对象,对其执行某些操做。
// 打印苹果重量 Consumer<Apple> appleWeighter = a -> System.out.println("The apple weights " + a.getWeight() + " grams"); appleWeighter.accept(new Apple(200)); // The apple weights 200 grams
public interface Supplier<T> { T get(); }
get:() -> T
,无入参,返回一个泛型T对象。
适用场景:定义类型T的对象的生产规则。
Consumer<Apple> appleWeighter = (a) -> System.out.println("The apple weights " + a.getWeight() + " grams"); // 生产200克的苹果 Supplier<Apple> heavyAppleSupplier = () -> new Apple(200); appleWeighter.accept(heavyAppleSupplier.get());
public interface Function<T, R> { R apply(T t); }
apply:T -> R
,接受一个泛型T对象,返回一个泛型R对象
适用场景:将输入对象转换输出。
double unitPrice = 0.01; // 计算苹果价格 Function<Apple, Double> priceAppleFunction = a -> a.getWeight() * unitPrice; priceAppleFunction.apply(new Apple(100)); // 1
这里作个补充,上面这段代码特别的地方在于使用到了外部的局部变量。Lambda表达式使用外部变量有什么要求?对于Lambda表达式所在的主体(类)的实例变量和静态变量,能够无限制使用,但局部变量必须显示声明为final或其实是final的。声明为final好理解,什么是其实是final的,意思就是不能被代码进行修改,好比这里的unitPrice虽然没有声明为final,但后续的代码并无修改该变量,因此实际上也是final的。感兴趣的读者能够本身试下,对unitPrice进行修改,看下会发生什么。
public interface Comparator<T> { int compare(T o1, T o2); }
compare:(T, T) -> int
,两个泛型T类型的入参,返回int类型
适用场景:比较两个对象
Comparator<Apple> weightComparator = (a1, a2) -> a1.getWeight() - a2.getWeight(); weightComparator.compare(new Apple(100), new Apple(150)); // -1
Java还提供了一种更简洁的写法,先上示例:
Function<Apple, Integer> weightor = a -> a.getWeight();
可改写为:
Function<Apple, Integer> weightor = Apple::getWeight;
这种写法被称做方法引用,仅当在Lambda表达式中直接调用了一个方法时可使用。其写法为目标引用::方法名称
。
根据目标引用可分为三类:
(1)指向静态方法的方法引用
目标引用为类,调用其静态方法,例如:
Function<String, Integer> fun = s -> Integer.parseInt(s);
调用了 Integer
的静态方法 parseInt
,可写为:
Function<String, Integer> fun = Integer::parseInt;
(2)指向任意类型实例方法的方法引用
目标引用为实例对象,调用其实例方法,例如:
Function<String, Integer> fun = s -> s.length();
调用了 String
类型实例 s
的 length
方法,可写为:
Function<String, Integer> fun = String::length;
目标引用写实例的类型。和第一种写法相同,只不过第一种的方法是静态方法,这里是实例方法。
(3)指向现存外部对象实例方法的方法引用
目标引用为现存外部对象,调用其实例方法,例如:
String s = "草捏子"; Supplier<Integer> len = () -> s.length();
调用了局部变量 s
的 length
方法,可写为:
String s = "草捏子"; Supplier<Integer> len = s::length;
目标引用写变量名,区别于前两种。
以前的例子都是使用的单个Lambda表达式,如今咱们把多个Lambda表达式组合在一块儿,构建更复杂一点的表达式。
咱们使用 Comparator
对苹果进行排序,按重量从小到大:
List<Apple> apples = Arrays.asList(new Apple("red", 50), new Apple("red", 100), new Apple("green", 100)); apples.sort(Comparator.comparing(Apple::getWeight));
对 Comparator
的静态方法comparing
简单介绍下,接受一个 Function
类型的参数,返回一个 Comparator
类型的实例,定义以下:
public static <T, U extends Comparable<? super U>> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); }
经过使用compareTo
,实现了重量从小到大的排序,那想按重量从大到小排序,怎么办呢?可使用 Comparator
的 reversed
方法:
apples.sort(Comparator.comparing(Apple::getWeight).reversed());
reversed
的实现以下:
default Comparator<T> reversed() { return Collections.reverseOrder(this); }
使用工具类获得一个反序的 Comparator
。你可能会好奇Comparator
做为一个接口,reversed
方法能够有具体的实现,接口的实例方法应该都是抽象方法,那它仍是一个有效的函数式接口吗,或者说仍是一个有效的接口吗?回想下第二节的内容,函数式接口是只定义一个抽象方法的接口。Comparator
的抽象方法只有一个 compare
,其余是具体方法,因此是合法的函数式接口。那么接口中为何能定义具体方法呢?Java8 以前是不支持的,但在 Java8 中引入了 default
关键字。当在接口中用default
声明一个方法时,容许它是一个具体方法。这样的好处在于,咱们能够在Lambda表达式以后直接跟上一个具体方法,对Lambda表达式加强,实现更复杂的功能。在后文介绍的用于复合表达式的方法都是接口中的 default
方法。
下面咱们试着实现更复杂的排序,在按重量从大到小排序后,按颜色排序:
apples.sort(Comparator.comparing(Apple::getWeight).reversed()); apples.sort(Comparator.comparing(Apple::getColor));
前后用两个Comparator
。而使用 Comparator
的 thenComparing
方法能够继续链接一个 Comparator
,从而构建更复杂的排序:
apples.sort(Comparator.comparing(Apple::getWeight) .reversed().thenComparing(Apple::getColor));
Predicate
的 test
方法 (T) -> boolean
返回一个布尔表达式。相似 Java 在为布尔表达式提供的与或非,Predicate
中也有对应的方法 and
、or
、negate
。例如:
// 重的苹果 Predicate<Apple> heavyApple = a -> a.getWeight() > 100; // 红的苹果 Predicate<Apple> redApple = a -> a.getColor().equals("red"); // 轻的苹果 Predicate<Apple> lightApple = heavyApple.negate(); // 不红的苹果 Predicate<Apple> nonRedApple = redApple.negate(); // 重且红的苹果 Predicate<Apple> heavyAndRedApple = heavyApple.and(redApple); // 重或红的苹果 Predicate<Apple> heavyOrRedApple = heavyApple.or(redApple);
Function
(T) -> R
,对输入作映射。咱们经过将多个Function
进行组合,实现将一个Function
的输出做为另外一个Function
的输入,是否是有管道的感受。下面请看具体的方法。
andThen
方法,a.andThen(b)
,将先执行a,再执行b。
Function<Integer, Integer> f = x -> x + 1; Function<Integer, Integer> g = x -> x * 2; Function<Integer, Integer> h = f.andThen(g); int result = h.apply(1); // 4
compose
方法,a.compose(b)
,将先执行b,再执行a。
Function<Integer, Integer> f = x -> x + 1; Function<Integer, Integer> g = x -> x * 2; Function<Integer, Integer> h = f.compose(g); int result = h.apply(1); // 3