我有一个List<TestBuilder> testBuilders;
Test具有Test类型的函数构建
我做了 testBuilders.stream().map(Test::build()).collect()
我想在上面收集 array of Test i.e Test[]
我不确定会发生什么 collect function
使用终端操作Stream::toArray
将项目序列打包到一个数组中。但是,您必须定义一个提供的生成器IntFunction<A[]>
来分配返回数组的类型:
Test[] array = testBuilders.stream().map(Test::build()).toArray(size -> new Test[size]);
size -> new Test[size]
应将lambda表达式替换为方法参考:
Test[] array = testBuilders.stream().map(Test::build()).toArray(Test[]::new);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句