黑马程序员技术交流社区

标题: Stream流使用示例 [打印本页]

作者: luffysan    时间: 2019-10-10 14:30
标题: Stream流使用示例
  1. public int preferential(String username) {
  2.     //选中的订单清单
  3.     List<OrderItem> orderItemList = findCartList(username).stream().
  4.             filter(cart -> (boolean) cart.get("checked") == true).
  5.             map(cart -> (OrderItem) cart.get("item")).collect(Collectors.toList());

  6.     //聚合统计每种category的总价格,封装为map<categoryId,totalMoney>
  7.     Map<Integer, IntSummaryStatistics> cartMap = orderItemList.stream().
  8.             collect(Collectors.groupingBy(OrderItem::getCategoryId3,
  9.                     Collectors.summarizingInt(OrderItem::getMoney)));

  10.     //遍历每种category的优惠金额并累加
  11.     int totalPreMoney = 0;
  12.     for (Integer categoryId : cartMap.keySet()) {
  13.         int preMoney = preferentialService.findPreMoneyByCategoryId(categoryId.longValue(),
  14.                 (int) cartMap.get(categoryId).getSum());
  15.         System.out.println("分类:"+categoryId+"...  "+"优惠:"+preMoney);
  16.         totalPreMoney+=preMoney;
  17.     }
  18.     return totalPreMoney;
  19. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2