1 lambda依赖上下文
1.1 测试lambda表达式
public classTest3 {public static voidmain(String[] args) {
TheInterface1 t1= () ->{};
TheInterface2 t2= () ->{};
System.out.println(t1.getClass().getInterfaces()[0]);
System.out.println(t2.getClass().getInterfaces()[0]);
}
}
@FunctionalInterfaceinterfaceTheInterface1{voidmyMethod1();
}interfaceTheInterface2{voidmyMethod2();
}
lambda表达式依赖于上下文。是个对象。是函数式接口的实现类
1.2 结合Thread理解
开启一个线程
new Thread(newRunnable() {
@Overridepublic voidrun() {
}
}).start();new Thread(() -> System.out.println("hello world")).start();
1.3 理解<>,菱形语法
Explicit(显示) type argument String can be replaced with <>
1.4 理解Stream
/*** Returns a sequential(串形) {@codeStream} with this collection as its source.
*
*
This method should be overridden when the {@link#spliterator()}
* method cannot return a spliterator that is {@codeIMMUTABLE(并发的)},
* {@codeCONCURRENT(不可变的)}, or late-binding(延迟绑定的). (See {@link#spliterator()}
* for details.)
*
* @implSpec
* The default implementation creates a sequential {@codeStream} from the
* collection's {@codeSpliterator}.
*
*@returna sequential {@codeStream} over the elements in this collection
*@since1.8*/
同上:调用stream后,返回一个Stream对象
/*** Returns a stream consisting(组成) of the results of applying the given
* function to the elements of this stream.
*
*
This is an intermediate
* operation(中间操作).
*
*@param The element type of the new stream
*@parammapper a non-interfering(无干扰),
* function to apply to each element
*@returnthe new stream*/
/*** Represents(代表) a function that accepts one argument and produces a result.
*
*
This is a functional interface
* whose functional method is {@link#apply(Object)}.
*
*@param the type of the input to the function
*@param the type of the result of the function
*
*@since1.8*/
/*** Applies(应用) this function to the given argument.
*
*@paramt the function argument
*@returnthe function result*/
可以看到map的参数是Function,是个函数式接口。本就可以使用lambda/方法引用实现。可以点击上述标注的地方,确认具体实现的地方。。。
使用?String::toUpperCase是方法引用,类名直接引用共有方法,类名.共有方法不可行,因为方法不是静态方法,但是方法引用就可以
1.5 理解语句和表达式
2 lambda,传递的是一个行为
2.1 传递行为
2.2 两个function的串联
/*** Returns a composed(组合) function that first applies the {@codebefore}
* 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 the type of input to the {@codebefore} function, and to the
* composed function
*@parambefore the function to apply before this function is applied
*@returna composed function that first applies the {@codebefore}
* function and then applies this function
*@throwsNullPointerException if before is null
*
*@see#andThen(Function)*/
/*** Returns a composed function that first applies this function to
* its input, and then applies the {@codeafter} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
*@param the type of output of the {@codeafter} function, and of the
* composed function
*@paramafter the function to apply after this function is applied
*@returna composed function that first applies this function and then
* applies the {@codeafter} function
*@throwsNullPointerException if after is null
*
*@see#compose(Function)*/
2.3 Bifunction,输入两个参数,返回一个值
/*** Represents a function that accepts two arguments and produces a result.
* This is the two-arity specialization(两个参数的特例) of {@linkFunction}.
*
*
This is a functional interface
* whose functional method is {@link#apply(Object, Object)}.
*
*@param the type of the first argument to the function
*@param the type of the second argument to the function
*@param the type of the result of the function
*
*@seeFunction
*@since1.8*/
/*** Returns a composed function that first applies this function to
* its input, and then applies the {@codeafter} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
*@param the type of output of the {@codeafter} function, and of the
* composed function
*@paramafter the function to apply after this function is applied
*@returna composed function that first applies this function and then
* applies the {@codeafter} function
*@throwsNullPointerException if after is null*/