如何使用 java 8 中的流将集合/数组转换为 JSONArray [英] How to convert a collection/ array into JSONArray using stream in java 8
问题描述
我有一个双数组,我需要使用 java streams
将数组转换为 JSONArray
.我尝试使用 forEach(共享可变性)导致数据丢失.
public static JSONArray arrayToJson(double[] array) 抛出 JSONException{JSONArray jsonArray = new JSONArray();Arrays.stream(数组).forEach(jsonArray::put);返回 jsonArray;}
有什么办法可以使用流创建 JSONArray
吗?
你的代码有效,但你可以写出这样的东西 (jdk 8+
):
return Arrays.stream(array).collect(Collector.of(JSONArray::new,//初始化累加器JSONArray::put,//处理每个元素JSONArray::put//confluence 2个累加器并行执行));
再举一个例子(从List
创建一个String
):
List列表 = ...字符串 str = list.stream().collect(Collector.of(StringBuilder::new,StringBuilder::append,StringBuilder::append,StringBuilder::toString//累加器的最后一个动作(可选)));
<块引用>
看起来不错,但编译器抱怨:error: incompatible throw types JSONException in method reference .collect(Collector.of(JSONArray::new, JSONArray::put, JSONArray::put)
>
我在 jdk 13.0.1
和 JSON 20190722
上检查了这个,除了 预期的 3 个参数外没有发现问题,但发现了 1 个
在 .collect(...)
.
(Gradle : 实现组:'org.json',名称:'json',版本:'20190722'
)
修复:
public static JSONArray arrayToJson(double[] array) 抛出 JSONException {返回 Arrays.stream(array).collect(JSONArray::new,JSONArray::put,(ja1, ja2) ->{for(最终对象o:ja2){ja1.put(o);}});}
注意:组合器不能只是对 JSONArray::put
的方法引用,因为这只会将一个数组放入另一个数组中(例如 [[]]
) 而不是按照所需的行为实际组合它们.
Im having a double array , I need to convert the array into a JSONArray
using java streams
. I tried using forEach (shared mutability) which leads to loss of data.
public static JSONArray arrayToJson(double[] array) throws JSONException{
JSONArray jsonArray = new JSONArray();
Arrays.stream(array)
.forEach(jsonArray::put);
return jsonArray;
}
Is there any way I could to create JSONArray
using streams?
Your code works, but you can write something like this (jdk 8+
):
return Arrays.stream(array)
.collect(Collector.of(
JSONArray::new, //init accumulator
JSONArray::put, //processing each element
JSONArray::put //confluence 2 accumulators in parallel execution
));
one more example (create a String
from List<String>
):
List<String> list = ...
String str = list.stream()
.collect(Collector.of(
StringBuilder::new,
StringBuilder::append,
StringBuilder::append,
StringBuilder::toString //last action of the accumulator (optional)
));
Looks nice, but compiler complaints:
error: incompatible thrown types JSONException in method reference .collect(Collector.of(JSONArray::new, JSONArray::put, JSONArray::put)
I checked this on jdk 13.0.1
and JSON 20190722
and didn't find problems except of Expected 3 arguments, but found 1
in .collect(...)
.
(Gradle : implementation group: 'org.json', name: 'json', version: '20190722'
)
Fix:
public static JSONArray arrayToJson(double[] array) throws JSONException {
return Arrays.stream(array).collect(
JSONArray::new,
JSONArray::put,
(ja1, ja2) -> {
for (final Object o : ja2) {
ja1.put(o);
}
}
);
}
Note: The combiner cannot be a method reference to just JSONArray::put
because this will just put one array into the other (e.g. [[]]
) instead of actually combining them as is the desired behavior.
这篇关于如何使用 java 8 中的流将集合/数组转换为 JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!