stream()優點:
1:無存儲。stream不是一種數據結構,它只是某種數據源的一個視圖,數據源可以是一個數組, Java容器或I/O channel等。
2:爲函數式編程而生。對stream的任何修改都不會修改背後的數據源,比如對stream執行過濾操作並不會刪除被過濾的元素,而是會產生一個不包含被過濾元素的新stream。
3:惰式執行。stream上的操作並不會立即執行,只有等到用戶真正需要結果的時候纔會執行。
4:可消費性。stream只能被“消費”一次,一旦遍歷過就會失效,就像容器的迭代器那樣,想要再次遍歷必須重新生成。
List
List<OrderInfoListDto> orderList = orderListInfo.getDataList();
List<CommodityOrderVO> collect=orderList.stream().map(orderInfo->{
CommodityOrderVO orderInfoListVo = new CommodityOrderVO();
BeanUtils.copyProperties(orderInfo, orderInfoListVo);
return orderInfoListVo;
}).collect(Collectors.toList());
Map
Map<Integer, List<String>> map = data.stream().collect(
Collectors.groupingBy(ScTaskDetailVO::getCommunityId,
Collectors.mapping(ScTaskDetailVO::getBuildingNo, Collectors.toList())));
List->map
Map<Long, UserInfo> mapsUserInfo =
list.stream().collect
(Collectors.toMap(UserInfo::getEnterpriseId,
po -> po, (key1, key2) -> key2));