【问题标题】:Recursively increase the same nested map values if keys are already exists如果键已经存在,则递归增加相同的嵌套映射值
【发布时间】:2019-12-13 06:33:16
【问题描述】:

我的parentMap 如下所示。

HashMap<String, Integer>>> parentMap = {disabled={account={test1=22}, group={test2=10}}}

我想做的是,如果operationType=disabledobjectType=accountgroup 等和testName=test1test2 等,那么我想将test1 的计数增加1。

我必须更新同一张地图,以便最后我应该得到一些统计数据,比如有 22 个 tests 案例 objectType=account 和 10 个 tests 案例 objectType=group 等被禁用

我在下面尝试了一些方法,但它进入了无限循环,因为我将值放入地图并再次对其进行迭代。

private HashMap<String, HashMap<String, HashMap<String, Integer>>> countTags(String statType, String objectType,
            String opType, HashMap<String, HashMap<String, HashMap<String, Integer>>> parentMap) {
        if (!Util.isEmpty(parentMap)) {

            //created new map to avoid infinite loop here but no luck :(

            HashMap<String, HashMap<String, Integer>> objMap = new HashMap<>();
            objMap.putAll(parentMap.get(statType));

            Iterator<Entry<String, HashMap<String, Integer>>> it = objMap.entrySet().iterator();
            while (it.hasNext()) {

                Entry<String, HashMap<String, Integer>> operationEntry = it.next();
                HashMap<String, Integer> operationMap = operationEntry.getValue();
                Set<String> opKeySet = operationMap.keySet();
                Iterator<String> opIt = opKeySet.iterator();

                while (opIt.hasNext()) {
                    parentMap.put(statType, countTags(objectType, opType, operationMap));
                }
            }
        } else {
            parentMap.put(statType, countTags(objectType, opType, new HashMap<String, Integer>()));
        }
        return parentMap;
    }

    private HashMap<String, HashMap<String, Integer>> countTags(String objectType, String opType, HashMap<String, Integer> tagMap) {
        int testRepeatCount = tagMap.get(opType) != null ? tagMap.get(opType) : 0;
        tagMap.put(opType, 1 + testRepeatCount);
        HashMap<String, HashMap<String, Integer>> objMap = new HashMap<>();
        objMap.put(objectType, tagMap);
        return objMap;
    }

我发现 a.compute(key, (k, v) -&gt; v == null ? 1 : v + 1); 也有一些建议 Java map.get(key) - automatically do put(key) and return if key doesn't exist? 但我可以得到一些帮助,我应该如何在这里以最佳方式实现我想要的结果?

【问题讨论】:

  • 是的,你从不打电话给opIt.next()
  • 我明白了。像parentMap.computeIfPresent("disable", (k, v) -&gt; v + 1) 这样的事情会帮助我吗?????
  • 不知道,但不会删除无限循环while (opIt.hasNext())
  • 在你的第一个代码 sn-ps 中,右尖括号是不平衡的。

标签: java data-structures collections recursive-datastructures


【解决方案1】:

我终于摆脱了自己的if_else 混乱。这就是我的最终方法的样子。这对我有帮助Java append `HashMap` values to existing HashMap if key matches

private HashMap<String, HashMap<String, HashMap<String, Integer>>> countTags(String statType, String objectType,
        String opType, HashMap<String, HashMap<String, HashMap<String, Integer>>> parentMap) {
    if (!Util.isEmpty(parentMap) && parentMap.containsKey(statType)) {
        // if objType is present, count the tags
        if (parentMap.get(statType).containsKey(objectType)) {
            HashMap<String, Integer> objMap = parentMap.get(statType).get(objectType);
            HashMap<String, Integer> map = countTags(objectType, opType, objMap).get(objectType);
            parentMap.get(statType).get(objectType).putAll(map);
        } else {
            // if objType isn't present, add that objType and count the tags
            HashMap<String, HashMap<String, Integer>> map = countTags(objectType, opType,
                    new HashMap<String, Integer>());
            parentMap.get(statType).put(objectType, map.get(objectType));
        }
    } else {
        // first time add the new tag to calculate it's object/operation wise
        // distribution
        parentMap.put(statType, countTags(objectType, opType, new HashMap<String, Integer>()));
    }
    return parentMap;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-28
    • 2022-12-05
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多