Java 8 新特性对比
前景
找到子集 or 兄弟
多规格商品
private List<Commodity> toCommodityRepresentation(List<Commodity> commodities){ List<CommodityStock> stocks=commodityStockMapper.findByIds(commodities); List<Commodity> commodityList = commodities.stream().filter(w -> w.getItemId() != 0).map(w -> {
//同itemid 不同ID List<CommodityStock> goodsItems = stocks.stream().filter(localW -> localW.getId() != w.getId() && localW.getItemId() == w.getItemId()).collect(Collectors.toList()); if(goodsItems.size() > 1){ logger.error("数据错误,同一条码最多只能存在两种规格,scp_item id:{}",w.getId()); } if(goodsItems.size() > 0){ w.setCommodityStock(goodsItems.get(0)); } return w; }).collect(Collectors.toList()); return commodityList; }
private List<Commodity> toCommodityRepresentation(List<Commodity> commodities){
List<CommodityStock> stocks=commodityStockMapper.findByIds(commodities);
Map<Long,CommodityStock> boxMap=new HashMap<>(); Map<Long,CommodityStock> scatterMap=new HashMap<>(); for (int i = 0; i < stocks.size(); i++) { CommodityStock stock=stocks.get(i); int bulk=stock.getBulk(); long itemId=stock.getItemId(); if(bulk==0){ boxMap.put(itemId,stock); }else{ scatterMap.put(itemId,stock); } } for (int i = 0; i <commodities.size() ; i++) { Commodity commodity=commodities.get(i); int bulk=commodity.getBulk(); long itemId=commodity.getItemId(); if(bulk==0 && !scatterMap.isEmpty() && scatterMap.containsKey(itemId)){ commodity.setCommodityStock(scatterMap.get(itemId)); } if(bulk==1 && !boxMap.isEmpty() && boxMap.containsKey(itemId)){ commodity.setCommodityStock(boxMap.get(itemId)); } } return commodities; }