I have a bunch of methods where the only difference is a Stream reduction operation :
static Stream<TimeValues> avg(Stream<TimeValues> data, int batchSize) {
return data
.collect(SlidingCollector.batch(batchSize))
.stream()
.map(tvl -> {
OptionalDouble od = tvl.stream()
...
.mapToDouble(tv -> tv.doubleValue())
.average(); // <-------
...
});
}
static Stream<TimeValues> max(Stream<TimeValues> data, int batchSize) {
return data
.collect(SlidingCollector.batch(batchSize))
.stream()
.map(tvl -> {
OptionalDouble od = tvl.stream()
...
.mapToDouble(tv -> tv.doubleValue())
.max(); // <-------
...
});
}
How to factorize this code and have a parametrized reduction operation (min, max, average, sum) ?
Thanks in advance.