更新时间:2023-09-26 23:02:52
您的代码有效,但您可以这样写:
Your code works, but you can write something like this:
return Arrays.stream(array)
.collect(Collector.of(
JSONArray::new, //init accumulator
JSONArray::put, //processing each element
JSONArray::put //confluence 2 accumulators in parallel execution
));
再来一个例子(从一个 String
创建一个列表< String>
):
one more example (create a String
from List<String>
):
List<String> list = ...
String str = this.list
.stream()
.collect(Collector.of(
StringBuilder::new,
(b ,s) -> b.append(s),
(b1, b2) -> b1.append(b2),
StringBuilder::toString //last action of the accumulator (optional)
));