I want to store an Integer or a Double, both extending from Number, into a HashMap, and retrieve them without casting. The reason is that I’ll eventually have many have many different subtypes (I am only using Number/Integer for illustration), and I don’t want to have to write a new method when a different subtype is introduced. Can I structure and call getFromMap() below without having to do the cast to Double or Integer ?
public class MapOfContent {
static Map<String, Number> hm = new HashMap<String, Number>();
public static void main(String[] args) {
addToMap(hm, Integer.valueOf(3), "An Integer");
addToMap(hm, Double.valueOf(3.4), "A Double");
Double d;
d = (Double) getFromMap(hm, "A Double");
//d = getFromMap(hm, "A Double");
System.out.println("Result-> " + d);
}
private static void addToMap(Map<String, ? super Number> hashmap, Number c, String key) {
hashmap.put(key, c);
}
private static <T> Number getFromMap(Map<String, ? extends Number> hashmap, String cmsKey) {
return hashmap.get(cmsKey);
}
}
There’s no way to actually make this approach genuinely typesafe; what if you tried
(Double) getFromMap(hm, "An Integer")? That should fail.One potential alternative might be to use the actual
Classobject as a key, for example using a GuavaClassToInstanceMap: