This is the example of the class I'm working on
public class TestReduce
{
private static Set<Integer> seed = ImmutableSet.of(1, 2);
public static void main(String args[]) {
List<Accumulator> accumulators = ImmutableList.of(new Accumulator(ImmutableSet.of(5, 6)), new Accumulator(ImmutableSet.of(7, 8)));
accumulators.stream()
.forEach(a -> {
seed = a.combineResult(seed);
});
System.out.println(seed);
}
}
class Accumulator
{
public Accumulator(Set<Integer> integers)
{
accumulatedNumbers = integers;
}
public Set<Integer> combineResult(Set<Integer> numbers) {
// Do some manipulation for the numbers
return (the new numbers);
}
private Set<Integer> accumulatedNumbers;
}
I would like to reduce all of the Accumulators to just a set of numbers but with the initial value. However, I cannot change the signature of the method combineResult. In the example, I did this by just using forEach but I'm not sure if there's a cleaner way or java stream way to achieve this? I tried using reduce but I couldn't quite get the arguments of the reduce right.