I want to create a generic method to calculate summation of values in the given List.
Instead of writing code below:
final BigDecimal spend = forecasts.stream()
.filter(forecast -> Objects.nonNull(forecast.getPrice()))
.map(PartForecastSupplierDto::getPrice)
.reduce(BigDecimal.ZERO, BigDecimal::add);
I want to write code like this:
public <T, R> R sum(Collection<T> collection,
Predicate<? super T> predicate,
Function<? super T, ? extends R> mapper,
R identity, final BinaryOperator<R> accumulator) {
return CollectionUtils.emptyIfNull(collection)
.stream()
.filter(predicate)
.map(mapper)
.reduce(identity, accumulator);
}
However, I'm getting an error like this:
How can I fix this error?
