我是靠谱客的博主 魁梧春天,这篇文章主要介绍两个长度不同的list Object数据进行相加运算,现在分享给大家,希望可以做个参考。

需求:两个list内object数据进项相加,获得新的数据,以下是我的代码部分:

 List<MoneyCountBean> counttradeamt = commonService.getList("counttradeamt", bean);
            List<MoneyCountBean> countrefundamt = commonService.getList("countrefundamt", bean);
            List<JSONObject> jsonList = new ArrayList<JSONObject>();
            for (MoneyCountBean tradeamt : counttradeamt) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", tradeamt.getId());
                jsonObject.put("count", tradeamt.getCounttradeamt());
                jsonList.add(jsonObject);
            }
            for (MoneyCountBean fundamt : countrefundamt) {
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("id", fundamt.getId());
                jsonObject1.put("count", fundamt.getCountrefundamt());
                jsonList.add(jsonObject1);
            }
            List<JSONObject> jsonList2 = new ArrayList<JSONObject>();
            Set<String> set = new HashSet<String>();
            for (JSONObject json : jsonList) {
                String id1 = json.getString("id");
                set.add(id1);
            }
            for (String str : set) {
                int count = 0;
                for (JSONObject json : jsonList) {
                    if (json.getString("id").equals(str)) {
                        count += (int) json.get("count");
                    }
                }
                JSONObject jsonObjects = new JSONObject();
                jsonObjects.put("id", str);
                jsonObjects.put("count", count);
                jsonList2.add(jsonObjects);
            }

好啦,以上就是我分享的全部内容啦,请大家多多指教!【ヾ(•ω•`)o~】

最后

以上就是魁梧春天最近收集整理的关于两个长度不同的list Object数据进行相加运算的全部内容,更多相关两个长度不同的list内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部