为什么 java 流 "reduce()" 累积相同的对象
why java stream "reduce()" accumulates the same object
ComparisonResults comparisonResults = requestsList
.parallelStream()
.map(item -> getResponse(item))
.map(item -> compareToBl(item))
.reduce(new ComparisonResults(), (result1, result2) ->
{
result1.addSingleResult(result2);
return result1;
});
当
private ComparisonResults compareToBl(CompleteRoutingResponseShort completeRoutingResponseShortFresh) {
...
ComparisonResults comparisonResults = new ComparisonResults();
...
return comparisonResults;
}
然而当我调试时:
.reduce(new ComparisonResults(), (result1, result2) ->
{
result1.addSingleResult(result2);
return result1;
});
我看到 result1
和 result2
总是同一个对象(
想法)
result1
等于 result2
addSingleResult
应该 return 一个修改为 this
副本的新对象,因此您应该将代码更改为:
.reduce(new ComparisonResults(), (result1, result2) ->
{
return result1.addSingleResult(result2);
});
否则,您总是return使用同一个实例(没有修改)。
The reduce operation always returns a new value. However, the
accumulator function also returns a new value every time it processes
an element of a stream. Suppose that you want to reduce the elements
of a stream to a more complex object, such as a collection. This might
hinder the performance of your application. If your reduce operation
involves adding elements to a collection, then every time your
accumulator function processes an element, it creates a new collection
that includes the element, which is inefficient. It would be more
efficient for you to update an existing collection instead. You can do
this with the Stream.collect method, which the next section describes.
ComparisonResults comparisonResults = requestsList
.parallelStream()
.map(item -> getResponse(item))
.map(item -> compareToBl(item))
.reduce(new ComparisonResults(), (result1, result2) ->
{
result1.addSingleResult(result2);
return result1;
});
当
private ComparisonResults compareToBl(CompleteRoutingResponseShort completeRoutingResponseShortFresh) {
...
ComparisonResults comparisonResults = new ComparisonResults();
...
return comparisonResults;
}
然而当我调试时:
.reduce(new ComparisonResults(), (result1, result2) ->
{
result1.addSingleResult(result2);
return result1;
});
我看到 result1
和 result2
总是同一个对象(
想法)
result1
等于 result2
addSingleResult
应该 return 一个修改为 this
副本的新对象,因此您应该将代码更改为:
.reduce(new ComparisonResults(), (result1, result2) ->
{
return result1.addSingleResult(result2);
});
否则,您总是return使用同一个实例(没有修改)。
The reduce operation always returns a new value. However, the accumulator function also returns a new value every time it processes an element of a stream. Suppose that you want to reduce the elements of a stream to a more complex object, such as a collection. This might hinder the performance of your application. If your reduce operation involves adding elements to a collection, then every time your accumulator function processes an element, it creates a new collection that includes the element, which is inefficient. It would be more efficient for you to update an existing collection instead. You can do this with the Stream.collect method, which the next section describes.