0

My parentMap is look like something below.

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

What I suppose to do is, if operationType=disabled and objectType=account or group etc and testName=test1 or test2 etc then I suppose to increase the count of test1 by 1.

I have to update the same map so that at the end I should get some statistic like there are 22 tests cases of objectType=account and 10 tests cases of objectType=group etc are disabled

I tried something below but it is going in infinite loop as I'm putting values in the map and iterating over it again.

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;
    }

I found a.compute(key, (k, v) -> v == null ? 1 : v + 1); also some suggestions here Java map.get(key) - automatically do put(key) and return if key doesn't exist? but can I get some help how optimally I should achieve my desired outcome here?

Swapnil Kotwal
  • 5,418
  • 6
  • 48
  • 92

1 Answers1

0

I finally get out of my own if_else mess. this is how my final method is look a like. This helped me here 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;
}
Swapnil Kotwal
  • 5,418
  • 6
  • 48
  • 92