一、FunctionCustom通用求和函数使用示例
特点:
简化代码量
防止集合及值的空指针
// 实例化函数
FunctionCustom<GetSalesDataReportsServiceOutputDto> functionCustom = new FunctionCustom<>();
//使用-》对 clueTodayCount 值求和
functionCustom.functionListLongSum(dtoEntityValue, en ->StringUtils.isNotNull(en.getClueTodayCount()),GetSalesDataReportsServiceOutputDto::getClueTodayCount)
二、求和函数
package com.bqx.bi.cluereport.util.functioncus;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
/**
* @author (kevin).
* @title bqx-bi.
* @package com.bqx.bi.cluereport.util.functioncus.
* @description 自定义函数.
* @date 2021/1/20.
*/
public class FunctionCustom<T> {
/**
* Long求和函数.
*
* @param targetList list
* @param predicate 过滤函数.
* @param function 求和函数.
* @return 求和值.
*/
public Long functionListLongSum(List<T> targetList, Predicate<? super T> predicate,
ToLongFunction<? super T> function) {
return Optional.ofNullable(targetList).orElse(Lists.newArrayList()).parallelStream().filter(predicate)
.mapToLong(function).sum();
}
/**
* int求和函数.
*
* @param targetList list
* @param predicate 过滤函数.
* @param function 求和函数.
* @return 求和值.
*/
public int functionListIntSum(List<T> targetList, Predicate<? super T> predicate, ToIntFunction<? super T> function) {
return Optional.ofNullable(targetList).orElse(Lists.newArrayList()).parallelStream().filter(predicate)
.mapToInt(function).sum();
}
/**
* Double求和函数.
*
* @param targetList list
* @param predicate 过滤函数.
* @param function 求和函数.
* @return 求和值.
*/
public Double functionListDoubleSum(List<T> targetList, Predicate<? super T> predicate,
ToDoubleFunction<? super T> function) {
return Optional.ofNullable(targetList).orElse(Lists.newArrayList()).parallelStream().filter(predicate)
.mapToDouble(function).sum();
}
}