List<WarehouseStockItemBean> warehouseStockItemBeanLists = new ArrayList<>();
List<WarehouseStockItemBean> warehouseStockItemBeans = new ArrayList<>();
Map<Long, WarehouseStockItemBean> map = new HashMap<>(); for (WarehouseStockItemBean warehouseStockItemBean : warehouseStockItemBeanLists) { Long id = warehouseStockItemBean.getCommodityId();//獲取commodity_id根據commodity_id進行合並 if (map.containsKey(id)) {//查看map里是否包含這個commodity_id WarehouseStockItemBean warehouseStockItem = map.get(id);拿到map里包含符合條件的數據 warehouseStockItem.setQuantity(warehouseStockItem.getQuantity().add(warehouseStockItemBean.getQuantity()));//將quantity進行相加 map.put(id, warehouseStockItem); } else { map.put(id, warehouseStockItemBean); } } for (Long temp : map.keySet()) { warehouseStockItemBeans.add(map.get(temp)); }
