Java Generics erasure in Multimaps
generics, guava, java
Solution
public static void output(Multimap<? extends Number, String> map) {
for (Collection<String> strings : map.asMap().values()) {
output(strings);
}
}
Problem
I have two `Multimap`s of `String`s indexed by (i) `Integer`s and (ii) `Double`s and a routine to output lists of the `String`s. ``` public static void outputInteger(Multimap<Integer, String> map) { for (Integer key : map.keySet()) { Collection<String> strings = map.get(key); output(strings); } } public static void outputDouble(Multimap<Double, String> map) { for (Double key : map.keySet()) { Collection<String> strings = map.get(key); output(strings); } } ``` I would like to combine these into a single routine using `Number` as the superclass of `Integer` and `Double` ``` public static void outputNumber(Multimap<? extends Number, String> map) { for (Number key : map.keySet()) { Collection<String> ids = map.get(key); //** } } ``` but the asterisked line does not compile ``` The method get(capture#5-of ? extends Number) in the type Multimap<capture#5-of ? extends Number,String> is not applicable for the arguments (Number) ``` How do I tackle this?