Guava has something similar, it's called ClassToInstanceMap.
You can read more about it in a dedicated Wiki section.
But apparently what you need is a ClassToInstanceMultimap. There is none yet, but you could file a feature request.
Guava has something similar, it's called ClassToInstanceMap.
You can read more about it in a dedicated Wiki section.
But apparently what you need is a ClassToInstanceMultimap. There is none yet, but you could file a feature request.
I don't want to traverse the collection and cast every object, since the collection is too large.
I wouldn't worry about it because
- you should never need to do this
- generics are a compile time check so casting to a generic doesn't do anything at runtime.
e.g. If T extends Object, casting a Object to T doesn't do anything. Similarly cast Collection to Collection<T> doesn't do anything at runtime.
But imagine the map is Map<Class<T>, Collection<T>>, in which, the value is a very large collection.
If you imagine this will do something, casting is not the answer. You may need to convert the contents of the Collection but that is completely different in Java.