0

So I have this hashmap named "hm" which produces the following output(NOTE: this is just a selection) :

{1=35, 2=52, 3=61, 4=68, 5=68, 6=70, 7=70, 8=70, 9=70, 10=72, 11=72}

{1=35, 2=52, 3=61, 4=68, 5=70, 6=70, 7=70, 8=68, 9=72, 10=72, 11=72}

{1=35, 2=52, 3=61, 4=68, 5=68, 6=70, 7=70, 8=70, 9=72, 10=72, 11=72}

This output was created with the following code(NOTE : the rest of the class code is not shown here) :

private int scores;
HashMap<Integer,Integer> hm = new HashMap<>();

for (int i = 0; i < fileLines.length(); i++) {
    char character = fileLines.charAt(i);
    this.scores = character;
    int position = i +1;
    hm.put(position,this.scores);
 }
 System.out.println(hm);

What I am trying to do is put all these hashmaps together into one hashmap with as value the sum of the values per key. I am familiar with Python's defaultdict, but could not find an equivalent working example. I have searched for an answer and hit those answers below but they do not solve my problem.

How to calculate a value for each key of a HashMap?

what java collection that provides multiple values for the same key

is there a Java equivalent of Python's defaultdict?

The desired output would be :

{1=105, 2=156, 3=183 , 4=204 ,5=206 ..... and so on}

Eventually the average per position(key) has to be calculated but that is a problem I think I can fix on my own when I know how to do the above.

EDIT : The real output is much much bigger ! Think about 100+ of the hashmaps with more than 100 keys.

Community
  • 1
  • 1
user3309936
  • 25
  • 1
  • 8
  • 1
    What stops you from iterating over the entries of all 3 HashMaps and summing the values for each key? – Eran Jun 08 '16 at 10:37
  • If you know the keys are 1 to 11, just `for` loop and sum using `get()`. If you don't know the keys, but know all maps have the exact same keys, just iterate the keys from one of them, and sum using `get()`. – Andreas Jun 08 '16 at 10:37
  • Thank you I will try your suggestion ! – user3309936 Jun 08 '16 at 10:43

2 Answers2

3

Try with something like that

public Map<Integer, Integer> combine(List<Map<Integer, Integer>> maps) {
    Map<Integer, Integer> result = new HashMap<Integer, Integer>();
    for (Map<Integer, Integer> map : maps) {
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            int newValue = entry.getValue();
            Integer existingValue = result.get(entry.getKey());
            if (existingValue != null) {
                 newValue = newValue + existingValue;
            }
            result.put(entry.getKey(),  newValue);
        }
    }
    return result;
}

Basically:

  • Create a new map for the result
  • Iterate over each map
  • Take each element and if already present in the result increment the value, if not put it in the map
  • return the result
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
1
   newHashMap.put(key1,map1.get(key1)+map2.get(key1)+map3.get(key1));
Nikesh Joshi
  • 824
  • 6
  • 17
  • But what if I have 100 hashmaps ? that is the case here , this is just an example of a real tiny bit of the output . – user3309936 Jun 08 '16 at 10:41
  • I am just giving example for one you can do this, for all you should write loop. – Nikesh Joshi Jun 08 '16 at 10:44
  • @nikeshjoshi my point was only on how to choose the name of the variable, key1 seems that you loop only over the first map. If you loop over all the three map keys it works (if you don't loop twice on the same key). I didn't downvoted – Davide Lorenzo MARINO Jun 08 '16 at 10:47