java 通過map根據list某個字段進行合並


 

 

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));
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM