我有一个双精度数组,我需要使用java streams
将该数组转换为JSONArray
。我尝试使用forEach (共享可变性),但这会导致数据丢失。
public static JSONArray arrayToJson(double[] array) throws JSONException{
JSONArray jsonArray = new JSONArray();
Arrays.stream(array)
.forEach(jsonArray::put);
return jsonArray;
}
有没有办法使用streams创建JSONArray
?
发布于 2018-01-08 15:54:51
JSONArray
不是线程安全的。如果你使用的是并行流,你应该同步操作。
Arrays
.stream(array)
.parallel()
.forEach(e -> {
synchronized(jsonArray) {
jsonArray.put(e);
}
});
https://stackoverflow.com/questions/48145457
复制相似问题