黑马程序员技术交流社区
标题:
Stream流使用示例
[打印本页]
作者:
luffysan
时间:
2019-10-10 14:30
标题:
Stream流使用示例
public int preferential(String username) {
//选中的订单清单
List<OrderItem> orderItemList = findCartList(username).stream().
filter(cart -> (boolean) cart.get("checked") == true).
map(cart -> (OrderItem) cart.get("item")).collect(Collectors.toList());
//聚合统计每种category的总价格,封装为map<categoryId,totalMoney>
Map<Integer, IntSummaryStatistics> cartMap = orderItemList.stream().
collect(Collectors.groupingBy(OrderItem::getCategoryId3,
Collectors.summarizingInt(OrderItem::getMoney)));
//遍历每种category的优惠金额并累加
int totalPreMoney = 0;
for (Integer categoryId : cartMap.keySet()) {
int preMoney = preferentialService.findPreMoneyByCategoryId(categoryId.longValue(),
(int) cartMap.get(categoryId).getSum());
System.out.println("分类:"+categoryId+"... "+"优惠:"+preMoney);
totalPreMoney+=preMoney;
}
return totalPreMoney;
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2