List< Integer > 变为int[]:
list.stream().mapToInt(Integer::intValue).toArray()
mapToInt 方法:就是让list这个stream中的每个元素,调用里面的这个转为int方法的方法(Returns an {@code IntStream} consisting of the results of applying the
given function to the elements of this stream.)
IntStream mapToInt(ToIntFunction<? super T> mapper)
reduce
这个是reduce的官方说明,简洁易懂,它相当于下面这个循环。
identity就是初始值,每次和stream中的一个值进行一次转换,更新到identity上,然后再和下一个转换,再更新identity!直到最后输出最后的identity
例子:累加啥的就不说了,我这里用List< Character >变成List< String >
characters是一个List< Character >,这些character组合起来是一个string
flatMap:
这篇文章对flatMap解释的很好。可以参考:
https://zhuanlan.zhihu.com/p/103686124