I want to create a mapping from (a) class type to (b) long (the identifier of the object of the defined class type) to (c) the object itself.
I have the following:
protected HashMap<Class<?>, HashMap<Long, ?>> obj = new HashMap<Class<?>, HashMap<Long, ?>>();
Is it possible to somehow denote that the first ? must be of the same type than the second ?? I would expect something like this, but this is ofcourse not possible:
protected <T> HashMap<Class<T>, HashMap<Long, T>> obj = new HashMap<Class<T>, HashMap<Long, T>>();
As an alternative, you could use a small amount of not-type-safe code encapsulated in a way that enforces your constraint:
The
type.cast()ingetItem()isn’t necessary for the compiler to not complain, but it would help catch an object of the wrong type getting into the cache early.