- 集合分组(根基对象某属性)
Map<Integer, List<OrderDetail>> collect = list.stream().collect(
Collectors.groupingBy(OrderDetail::getProGoodsId));
- 计算分组中某字段之和
Map<String,Integer> goodsSum=list().stream().collect(Collectors.groupingBy(OrderDetail::getGoodsSku,Collectors.summingInt(SalesOrderDetail::getGoodsSum)));
- 获取分组后组的数量
int prdocutSize = list.stream().collect(Collectors.groupingBy(OrderDetail::getGoodsSku)).size();
- 计算分组后每组对象的数量
Map<String, Long> mobileMap = list.stream().collect(Collectors.groupingBy(Customer::getMobile, Collectors.counting()));
- 计算集合中某属性的和
list.stream().collect(Collectors.summingInt(OrderDetail::getGoodsSum)).intValue();
- List转Map(key重复时取第一个对象)
- 方式一
Map<String, SysUser> userMap=list.stream().collect(Collectors.toMap(SysUser::getLoginName,sysUser->sysUser,(key1,key2)->key1));
- 方式二
Map<Integer,OrderDataVo> orderDataMap = orderDatas.stream().collect(Collectors.groupingBy(OrderDataVo::getSkuId,Collectors.collectingAndThen(Collectors.toList(), value->value.get(0))));
- 将对象的某两个属性作为KEY:VALUE
- 方式一
Map<String,Integer> map1 = goodsVo.stream().collect(Collectors.toMap(pushGoodsInveroyVo ->pushGoodsInveroyVo.getSquareItemId(),PushGoodsInveroyVo::getNum));
- 方式二
Map<String,Integer> map2 = goodsVo.stream().collect(Collectors.toMap(PushGoodsInveroyVo::getSquareItemId, PushGoodsInveroyVo::getNum));
- 方式三
Map<String,Integer> map3 = goodsVo.stream().collect(Collectors.toMap(PushGoodsInveroyVo::getSquareItemId, PushGoodsInveroyVo::g
一只渣渣
发布了16 篇原创文章 · 获赞 0 · 访问量 2462
私信
关注