重用同一个数组而不是在<Watch Include="**\*.json" Exclude="node_modules\**\*.json;$(DefaultExcludes)" />
中声明是否有意义?
.collectInto
我们可以用以下代码替换上面的代码:
return loadSortedSparseNumbersObservable()
.collectInto(
new numbers[1000],
(array, number) -> {
int index = computeTrueIndex(...);
array[index] = number;
}
)
.map(array -> );
我不太喜欢改变对象。
答案 0 :(得分:1)
你的第二个例子有更多的操作符因此可能有更多的开销,当然阅读和理解起来更加丑陋。
您的示例中的大问题是返回的Observable
不能安全地重复使用。对同一Observable
的并发调用将在阵列上进行交互。安全的方法是为每个订阅创建一个新数组。使用collect
代替collectInto
:
return loadSortedSparseNumbersObservable()
.collect(
() -> new numbers[1000],
(array, number) -> {
int index = computeTrueIndex(...);
array[index] = number;
}
)
.map(array -> );
答案 1 :(得分:0)
就地修改阵列的关注点是线程安全性。 Java中的数组是传递引用,因此任何数组的重用/修改都可能导致程序其他部分出现并发问题。
但是,这个问题并未提供有关使用此特定数组的代码的结构或功能的见解,因此可能存在特定于应用程序的原因需要进行就地修改。同样,可能有一个非常令人信服的理由不这样做。