我是靠谱客的博主 舒适小伙,这篇文章主要介绍List相同字段相加的JAVA实现,现在分享给大家,希望可以做个参考。

//处理相同的套餐就整合在一起然后把价格加起来
                Map<String,OrderItem> hashMap = new HashMap<String, OrderItem>();
                for (OrderItem orderItem : orderItemList) {
                    //如果套餐名为空就不用加进去了
                    if (orderItem.getpItemName()!=null || !"".equals(orderItem.getpItemName())) {
                        //map里面相同的套餐名就把里面的价格就相加
                        if (hashMap.containsKey(orderItem.getpItemName())) {
                            double price = Double.parseDouble(orderItem.getPrice());
                            price+= Double.parseDouble(hashMap.get(orderItem.getpItemName()).getPrice());
                            hashMap.get(orderItem.getpItemName()).setPrice(String.valueOf(price));
                        } else {
                            hashMap.put(orderItem.getpItemName(), orderItem);
                        }
                    }
                }

               //保存在list里
                List<OrderItem> oiList = new ArrayList<OrderItem>();
                for(OrderItem o:hashMap.values()){
                    oiList.add(o);
                }

最后

以上就是舒适小伙最近收集整理的关于List相同字段相加的JAVA实现的全部内容,更多相关List相同字段相加内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部