r/javahelp • u/cas-san-dra • Nov 22 '22
Instantiating generic map
The compiler won't accept this:
final var cache = new HashMap<Class<?>, Map<String, ?>>();
this.logic = new AnInterface() {
public <T extends Enum> Map<Class<T>, Map<String, T>> getCache() {
return (Map<Class<T>, Map<String, T>>) cache;
}
};I can't change the definition of the method in the interface. And I need to return the same object from the method, otherwise it wouldn't be much of a cache.
0
Upvotes
1
u/ThisHaintsu Nov 23 '22
If you know that
cachedefinetely has the appropriate type paramaters you can always use the not very elegant way of stripping and then readding them: