I have a Map and I want to get the type of T from an instance of that Map. How can I do that?
e.g. I want to do something like:
Map map = new HashMap();
...
String vtype = map.getValueType().getClass().getName(); //I want to get Double here
Of course there's no such 'getValueType()' function in the API.
解决方案
You can't get it from the instance, because in Java generics the type parameter is available only at compile time, not at run time.
This is known as type erasure. A more formal definition is provided in the JLS.