Java集合 两个Map-value值求和

Java集合 两个Map-value值求和

应用场景
两个map中k值相同的value值求和返回map计算

代码案例

    /**
     * Map外层遍历
     */
    public Map<Integer, Map<Integer, Integer>> mapCount(Map<Integer, Map<Integer, Integer>> quoraListMapSource, Map<Integer, Map<Integer, Integer>> quoraListMapTarget) {
     
        for (Integer sourceKey : quoraListMapSource.keySet()) {
     
            if (quoraListMapTarget.containsKey(sourceKey)) {
     
                Map<Integer, Integer> integerMap = mapValueCount(quoraListMapSource.get(sourceKey), quoraListMapTarget.get(sourceKey));
                quoraListMapTarget.put(sourceKey, integerMap);
            }
        }
        return quoraListMapTarget;
    }

    /**
     * MapValue统计
     */
    public Map<Integer, Integer> mapValueCount(Map<Integer, Integer> quoraListMapSource, Map<Integer, Integer> quoraListMapTarget) {
     
        for (Integer sourceKey : quoraListMapSource.keySet()) {
     
            if (quoraListMapTarget.containsKey(sourceKey)) {
     
                quoraListMapTarget.put(sourceKey, quoraListMapSource.get(sourceKey) + quoraListMapTarget.get(sourceKey));
            }
        }
        return quoraListMapTarget;
    }

你可能感兴趣的:(基础框架,java)