I have this function:
/**
* Helper function that adds the values of b to the elements of a, treating
* all keys that exist in b but not in a, as existent in a with value 0. NB:
* It operates IN PLACE.
* @param a The {@link java.util.Map} which will hold the result
* @param b The {@link java.util.Map} which will be added to a
*/
private static void sumMaps(Map<?, Integer> a, Map<?,Integer> b)
{
for (Object key : b.keySet()) {
Integer currentCount = a.get(key);
a.put(key, currentCount == null ? b.get(key) : currentCount + b.get(key));
}
}
However, NetBeans highlights “key” in the final line of for, and gives me this error:
method put in class java.util.Map<K,V> cannot be applied to given types
required: capture #67 of?, java.lang.Integer
found: java.lang.Object, int
(the int is not the problem because of Java unpacking, I tried using Integers too but it didn’t work).
Specifying “?” for the key means that it could be of any type, but the keys must be the same for each Map for this method to work. So use the following instead: