我是靠谱客的博主 执着黄蜂,这篇文章主要介绍两个map,键相同的相加,算总和合计,现在分享给大家,希望可以做个参考。

List<Map<String, Object>> sum=new ArrayList<>();
        //将list里面的map的每一个相同的key的值相加
		Map<String,Object> mapAll = new HashMap<String,Object>();
		for(Map<String,Object> map1:list){
			for(Map.Entry<String, Object> entry:map1.entrySet()){
				String name = entry.getKey();
				Object score = entry.getValue();
				int sc = Integer.parseInt(String.valueOf(score));
				Object scoreAll = mapAll.get(entry.getKey());
				// 如果是String类型则不相加  其它类型同理
				if (scoreAll instanceof String)
					continue;
				if(scoreAll == null){
					mapAll.put(name, sc);
				}else{
					scoreAll = new Integer((((Integer)scoreAll).intValue() + ((Integer)sc).intValue()));
					mapAll.put(name, scoreAll);
				}
			}
		}

		sum.add(mapAll);

最后

以上就是执着黄蜂最近收集整理的关于两个map,键相同的相加,算总和合计的全部内容,更多相关两个map,键相同内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部