nested generic HashMap problem
I've looked at the Java generics tutorial, Langer's FAQ, and similar
online pages, but I'm still don't see what, if anything, I can do to
make the last line (marked with the comment) compile, other than do a
typecast and suppress the unchecked warning. Am I asking the impossible
or missing the obvious?
import java.util.HashMap;
public class Demo<T> {
private Class<T> base;
private Cache cache;
Demo(Class<T> b, Cache c) { base = b; cache = c; }
Class<T> getBaseClass() { return base; }
T get(long id) { return cache.get(this, id); }
}
class Cache {
private HashMap<Class<?>, HashMap<Long, ?>> maps
= new HashMap<Class<?>, HashMap<Long, ?>>();
public <T> T get(Demo<T> demo, long id) {
return getMap(demo).get(id);
}
public <T> void add(Demo<T> demo) {
maps.put(demo.getBaseClass(), new HashMap<Long, T>());
}
private <T> HashMap<Long, T> getMap(Demo<T> demo) {
return maps.get(demo.getBaseClass()); // incompatible types
}
}