Java-List集合字段求和函数

更新时间:2023-05-11 20:26:39 阅读: 评论:0

Java-List集合字段求和函数
⽂章⽬录
⼀、FunctionCustom通⽤求和函数使⽤⽰例
特点:
简化代码量
防⽌集合及值的空指针
// 实例化函数
FunctionCustom<GetSalesDataReportsServiceOutputDto> functionCustom = new FunctionCustom<>();
//使⽤-》对 clueTodayCount 值求和
functionCustom.functionListLongSum(dtoEntityValue, en ->StringUtils.ClueTodayCount()),GetSalesDataReportsServiceOutputDto::getClue Toda
yCount)
⼆、求和函数
package com.bqx.bi.cluereport.util.functioncus;
llect.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).wArrayList()).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).wArrayList()).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).wArrayList()).parallelStream().filter(predicate)
.mapToDouble(function).sum();
}
}

本文发布于:2023-05-11 20:26:39,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/589999.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:函数   求和   简化
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图