Here is some code which does not compile. I would like to hold arbitrary pairs of Class<?>
(key) and Map<Long, ObjectDescriptors<?>>
(values) inside the typeMap;
From other methods I want to use one of the value maps with a concrete type, but the algorithms should be generic. I wrote the getIdentityMap()
method to deliver a map with a concrete but generic type.
How must I modify the code to compile while being generic?
Thanks in advance.
private final Map<Class<?>, Map<Long, ObjectDescriptor<?>>> typeMap = new HashMap<Class<?>, Map<Long,ObjectDescriptor<?>>();
private <T> Map<Long, ObjectDescriptor<T>> getIdentityMap(Class<T> type) {
Map<Long, ObjectDescriptor<T>> identityMap = typeMap.get(type);
if (identityMap == null) {
identityMap = new HashMap<Long, ObjectDescriptor<T>>();
typeMap.put(type, identityMap);
}
return identityMap;
}
EDIT: Here are the compiler errors:
- In line:
Map<Long, ObjectDescriptor<OBJECT_TYPE>> identityMap = typeMap.get(type);
- compiler error:
Type mismatch: cannot convert from Map<Long,ObjectDescriptor<Object>> to Map<Long,ObjectDescriptor<OBJECT_TYPE>>
- compiler error:
- In line:
typeMap.put(type, identityMap);
- compiler error:
The method put(Class<?>, Map<Long,ObjectDescriptor<Object>>) in the type Map<Class<?>,Map<Long,ObjectDescriptor<Object>>> is not applicable for the arguments (Class<OBJECT_TYPE>, Map<Long,ObjectDescriptor<OBJECT_TYPE>>)
- compiler error:
EDIT: edited the generic types of the map attribute to ?