lagom代码中有大量的Lambda表达式,首先补习一下lambda表达式和函数式接口的相关知识。
一: 函数式接口:
- 函数式接口其实本质上还是一个接口,但是它是一种特殊的接口:
这种类型的接口,使得以其为参数的方法,可以在调用时,使用一个lambda表达式作为参数(比如new Thread(Runable), 中的参数)。
从另一个方面说,一旦我们调用某方法,可以传入lambda表达式作为参数,则这个方法的参数类型,必定是一个函数式的接口。
- 什么是函数式接口:
接口用@FunctionalInterface注解修饰,但不是必须的;
这个接口中,只能有一个函数需要被实现;
但是可以有默认方法(default修饰)与静态方法(static修饰,并可以提供static方式实现);
可以有 Object 中覆盖的方法,也就是 equals,toString,hashcode等方法。
Runnable接口就是一个函数式接口:
@FunctionalInterface public interface Runnable { /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object‘s * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */ public abstract void run(); }
可以自定义函数式接口:
这个接口由一个需要实现的函数,这个函数的参数是F,返回的是T;
@FunctionalInterface interface Converter<F, T> { T convert(F from); }
使用: 如下的lambda表达式(from) -> Integer.valueOf(from); 这个表达式的格式要和上面的函数匹配,也就是说F在这里是String类型,T是Integer类型,from就是F,Integer.valueof(from)的结果是integer,T就是integer;
Converter<String, Integer> converter = (from) -> Integer.valueOf(from);
Integer converted = converter.convert("123");
如下为一些已经实现的函数式接口:
// Function<T, R> -T作为输入,返回的R作为输出 Function<String,String> function = (x) -> {System.out.print(x+": ");return "Function";}; System.out.println(function.apply("hello world")); //Predicate<T> -T作为输入,返回的boolean值作为输出 Predicate<String> pre = (x) ->{System.out.print(x);return false;}; System.out.println(": "+pre.test("hello World")); //Consumer<T> - T作为输入,执行某种动作但没有返回值 Consumer<String> con = (x) -> {System.out.println(x);}; con.accept("hello world"); //Supplier<T> - 没有任何输入,返回T Supplier<String> supp = () -> {return "Supplier";}; System.out.println(supp.get()); //BinaryOperator<T> -两个T作为输入,返回一个T作为输出,对于“reduce”操作很有用 BinaryOperator<String> bina = (x,y) ->{System.out.print(x+" "+y);return "BinaryOperator";}; System.out.println(" "+bina.apply("hello ","world"));
这里以Function接口为例:
Function接口的需要实现的函数是"R apply(T t)", 参数是t, 返回值是R;
Function<String,String> function = (x) -> {System.out.print(x+": ");return "Function";}; 这里的T是String类型,R也是String类型; System.out.println(function.apply("hello world")); // Function.apply()返回的是String类型,满足System.out.println的参数需要;“hello world”
是String类型,也满足apply的参数需要。
/* * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.util.function; import java.util.Objects; /** * Represents a function that accepts one argument and produces a result. * * <p>This is a <a href="package-summary.html">functional interface</a> * whose functional method is {@link #apply(Object)}. * * @param <T> the type of the input to the function * @param <R> the type of the result of the function * * @since 1.8 */ @FunctionalInterface public interface Function<T, R> { /** * Applies this function to the given argument. * * @param t the function argument * @return the function result */ R apply(T t); /** * Returns a composed function that first applies the {@code before} * function to its input, and then applies this function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <V> the type of input to the {@code before} function, and to the * composed function * @param before the function to apply before this function is applied * @return a composed function that first applies the {@code before} * function and then applies this function * @throws NullPointerException if before is null * * @see #andThen(Function) */ default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } /** * Returns a composed function that first applies this function to * its input, and then applies the {@code after} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <V> the type of output of the {@code after} function, and of the * composed function * @param after the function to apply after this function is applied * @return a composed function that first applies this function and then * applies the {@code after} function * @throws NullPointerException if after is null * * @see #compose(Function) */ default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } /** * Returns a function that always returns its input argument. * * @param <T> the type of the input and output objects to the function * @return a function that always returns its input argument */ static <T> Function<T, T> identity() { return t -> t; } }
Lambda表达式
书写方法: e -> System.out.println( e )
1. 三部分构成
参数列表
符号 ->
函数体 : 有多个语句,可以用{} 包括, 如果需要返回值且只有一个语句,可以省略 return
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////