This question already has an answer here:
The method:
public static void incrementMapCounter( Map<Object,Number> tabulationMap, Object key ) {
Number value = 0;
if ( tabulationMap.containsKey(key) ) {
value = tabulationMap.get(key);
}
value = value.doubleValue() + new Double(1);
tabulationMap.put( key, value );
}
Call to the method:
Map<String,Long> counts = new HashMap<>();
String key = "foo-bar";
incrementMapCounter( counts, key );
Error (reformatted):
The method
incrementMapCounter(Map<Object,Number>, Object)
in ... is not applicable
for the arguments (Map<String,Long>, String)
The method signature is either a matching type or more generic:
- Map is a Map
- String is an Object (x2)
- Long is a Number
I'm a bit confused on this one.
Generics are invariant so the arguments will need to match the arguments passed in so that values can be added to the
Collection
It's the later two. String and Object are not the same type. Generics are not covariant, they are invariant. The types have to match exactly. Same with Long and Number.
For your method signature you might try:
Which can be called by: