Well, you can't do much about that without also providing T
in a way that code can look at.
The simplest approach at that point would probably be to keep a map of 0 values:
private static Map<Class<?>, Number> ZERO_VALUES = createZeroValues();
private static Map<Class<?>, Number> createZeroValues() {
Map<Class<?>, Number> ret = new HashMap<Class<?>, Number>();
ret.put(Integer.class, (int) 0);
ret.put(Short.class, (short) 0);
ret.put(Long.class, (long) 0);
// etc
}
Then:
private <T extends Number> T getValue (Map<Integer, T> map, Integer key, Class<T> clazz) {
return clazz.cast(map.containsKey(key) ? map.get(key) : ZERO_VALUES.get(clazz));
}
Then you'd unfortunately have to call it as:
Short b = getValue(bMap, 15, Short.class);
Basically this is a limitation of Java generics :(