java8中Lambda表达式多字段分组

单子段分组/多字段分组:

dataList.stream().collect(Collectors.groupingBy(Entity::getName));
dataList.stream().collect(Collectors.groupingBy(e -> e.getName() +"|" + e.getAge()));

代码:

List<Entity> list = new ArrayList<>();
Map<Integer, String> map = list.stream().collect(Collectors.toMap(Entity::getId, Entity::getType));
		

常用的lambda表达式:

**
 * List -> Map
 * 需要注意的是:
 * toMap 如果集合对象有重复的key,会报错Duplicate key ....
 *  apple1,apple12的id都为1。
 *  可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
 */
Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));

安照某一字段去重
list = list.stream().filter(distinctByKey(p -> ((ModCreditColumn) p).getFieldCode())).collect(Collectors.toList());




List<Double> unitNetValue = listIncreaseDto.stream().map(IncreaseDto :: getUnitNetValue).collect(Collectors.toList());

//求和  对象List
BigDecimal allFullMarketPrice = entityList.stream().filter(value -> value.getFullMarketPrice()!= null).map(SceneAnalysisRespVo::getFullMarketPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
 
 List<BigDecimal>  naturalDayList;
 BigDecimal total = naturalDayList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);



分组函数
Map<String, List<SceneAnalysisRespVo>> groupMap = total.getGroupList().stream().collect(Collectors.groupingBy(SceneAnalysisRespVo::getVmName));



//DV01之和
BigDecimal allDV01 = values.stream().filter(sceneAnalysisRespVo -> sceneAnalysisRespVo.getDv() != null).map(SceneAnalysisRespVo::getDv).reduce(BigDecimal.ZERO, BigDecimal::add);

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值