Skip to content
Advertisement

Reducing ArrayList using MyClass attributes

I have a class, MyClass, like this:

JavaScript

If I want to get the sum of amount in an ArrayList of MyClass items, usually I would do this:

JavaScript

But now to improve performance, I am trying to use Stream.reduce(). My approach:

JavaScript

Is there a better way to do so? And how would the reduction be affected if currAmount or nextAmount is null?

Advertisement

Answer

But now to improve performance, I am trying to use Stream.reduce()

Stream does not improve performance, the main idea using stream is benefit from functional programming. If you want to improve performance, please do proper benchmarking. Anyway, you can simplify the stream as below.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement