stream常用list轉map list轉list

1,【list轉map】
1.1
  Map<String, OrganizationVO> groupMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(groupList)){
            groupMap = groupList.stream().collect(MapCollectors.toMap(v->v.getPurchaseOrgCode(), v->v));
        }
1.2
return products.stream().collect(Collectors.toMap(k -> k.getProductCode(), v -> v, (a, b) -> b));

2,【map獲取value】
List<SelectionProductShop> productShopSupplier = productShopHashMap.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());
                    
3,【list轉list】
3.1
 List<ReqSupplierOrderCycleVO> queryExpectedTime = productShopSupplier.stream().map(v -> {
           ReqSupplierOrderCycleVO supplierOrderCycleVO = new ReqSupplierOrderCycleVO();
           supplierOrderCycleVO.setSupplierCode(v.getProductCode());
           supplierOrderCycleVO.setBigCategoryCode(v.getShopCode());
           supplierOrderCycleVO.setLocationCode(v.getShopCode());
           return supplierOrderCycleVO;
       }).collect(Collectors.toList());  

3.2
 List<ResFreshSmallAreaConfigVO> resultList = new ArrayList<>();
        List<FreshSmallAreaConfig> freshSmallAreaConfigs = freshSmallAreaConfigMapper.selectSearchSmallArea(smallArea);
        freshSmallAreaConfigs.forEach(freshSmallAreaConfig -> {
            resultList.add(new ResFreshSmallAreaConfigVO(freshSmallAreaConfig.getId(), freshSmallAreaConfig.getSmallArea()));
        });
        return R.success(resultList);	
		   
4,【list轉map<String,List<Object>>】    
獲取商品門店對應供應商列表
4.1 key多屬性
 private Map<String, List<String>> getSupplierCodeList(List<LocationProductKeyVO> params) {
        Map<String, List<String>> supplierCodeMap = new HashMap<>();
        params.forEach(keyVo -> {
            String key = StringUtil.contact(BizConstant.UNDERLINE, keyVo.getLocationCode(), keyVo.getProductCode());
            List<String> supplierCodeList = supplierCodeMap.containsKey(key) ? supplierCodeMap.get(key) : new ArrayList<>();
            supplierCodeList.add(keyVo.getSupplierCode());
            supplierCodeMap.put(key, supplierCodeList);
        });
        return supplierCodeMap;
    } 
4.2 key單屬性 
Map<String, List<SelectionProductShop>> shopMap = Maps.newHashMap();
shopMap =  list.stream().collect(Collectors.groupingBy(SelectionProductShop::getShopCode,LinkedHashMap::new, Collectors.toList()));
    
4.3 key多屬性            
Map<String, List<SelectionProductShop>> result = selectionProductShopList.stream().collect(Collectors.groupingBy(e -> e.getShopCode() + e.getProductCode()));

4.4
Map<String, List<ShopRepCartVO>> queryResult = Maps.newHashMap();
            shopRepCartVOS.stream().forEach(result -> {
                String key = StringUtil.contact(BizConstant.UNDERLINE, result.getShopCode(), result.getProductCode());
                List<ShopRepCartVO> vos = queryResult.containsKey(key) ? queryResult.get(key) : new ArrayList<>();
                vos.add(result);
                queryResult.put(key, vos);
            });         

5,循環賦值            
 List<ShopProductDetailVO> detailVOS = baseProductShopClient.getByProductCodeMult(vos).getResult();
        Map<String, ShopProductDetailVO> supplierMap = detailVOS.stream().collect(MapCollectors.toMap(v -> StringUtil.contact(BizConstant.UNDERLINE, v.getShopCode(), v.getProductCode()), v -> v));
        list.stream().forEach(v -> {
            ShopProductDetailVO detailVO = supplierMap.get(StringUtil.contact(BizConstant.UNDERLINE, v.getShopCode(), v.getProductCode()));
            v.setSupplierCode(detailVO.getSupplierCode());
            v.setSupplierName(detailVO.getSupplierName());
        });
        
6,獲取List<Object>屬性組合
List<String> shopProducts = listEs.stream().map(v -> v.getLocationCode() + StringUtilBuilder.UNDER_LINE + v.getProductCode()).collect(Collectors.toList());

7,字符串拼接
StringUtil.contact(BizConstant.UNDERLINE, v.getShopCode(), v.getProductCode())

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章