Fork me on GitHub

Java 8 Lambda 表达式

  • stream().filter()

过滤集合中元素,将符合条件的元素放到一个新的集合中返回

List<Role> firstChildRoleList = childRoleList.stream().filter(role -> role.getParentId().equals(currentRoleId)).collect(Collectors.toList());
  • stream().map()

将集合中的某一个属性,放到一个新的集合中返回

 List<Long> orgIds = tenantVO.getOrganizationVOList().stream().map(e -> e.getOrgId()).collect(Collectors.toList());

将集合中多个元素放到一个对象中,以一个新的集合返回

  List<CartDTO> cartDTOList = new OrderDTO().getOrderDetailList().stream()
                .map(e -> new CartDTO(e.getProductName(),e.getProductQuantity())).collect(Collectors.toList());

将集合中某一元素放到一个新的集合中返回:String::toLowerCase等同于x->x.toLowerCase()。

 List<String> productNameList = new OrderDTO().getOrderDetailList().stream().map(OrderDetail::getProductName).collect(Collectors.toList());

 

posted @ 2020-05-29 14:58  w_wz  阅读(186)  评论(0)    收藏  举报