参考文章
https://www.jianshu.com/p/cd694d2d8be5
Function里面的identity有什么用
源码
static <T> Function<T, T> identity() {
return t -> t;
}
就是为了简化表达式,-。-
之前你可能需要这样写
.stream.collect(Collectors.toMap(p->p.getId,p->p));
和这样是一样的
.stream.collect(Collectors.toMap(p->p.getId,Function.identity()));
说明问题
这就说明了一个问题:
返回值是这个
return t->t;
所以这里是新创建了一个对象,然后返回这个Function对象
不能作为方法体里面的参数,因为方法体里面没有定义的变量是无效的。
这个t->t表示的一个整体对象