I want to implement such a map,
Map<Class<T>, T> m;
in which, I can get a generic object of type T, given the class of T.
I know you may say, you can wrap a Map<String, Object> and use casting to archive this.
I know this.
But imagine the map is Map<Class<T>, Collection<T>>, in which, the value is a very large collection.
I don’t want to traverse the collection and cast every object, since the collection is too large.
So? what do I do?
I wouldn’t worry about it because
e.g. If
T extends Object, casting aObjecttoTdoesn’t do anything. Similarly castCollectiontoCollection<T>doesn’t do anything at runtime.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.