获取 java.util.Map 参数的泛型类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6148798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get generic type for java.util.Map parameter
提问by Sebi
public Object[] convertTo(Map source, Object[] destination) {
...
}
Is there a possibility to figure out the generic types (key / value) of my Map parameter via Reflection?
是否有可能通过反射找出我的 Map 参数的通用类型(键/值)?
回答by Jofkos
I know this question is old, but the best answer is wrong.
You can easily get the generic types via reflections. Here an example:
我知道这个问题很老,但最好的答案是错误的。
您可以通过反射轻松获取泛型类型。这里有一个例子:
private Map<String, Integer> genericTestMap = new HashMap<String, Integer>();
public static void main(String[] args) {
try {
Field testMap = Test.class.getDeclaredField("genericTestMap");
testMap.setAccessible(true);
ParameterizedType type = (ParameterizedType) testMap.getGenericType();
Type key = type.getActualTypeArguments()[0];
System.out.println("Key: " + key);
Type value = type.getActualTypeArguments()[1];
System.out.println("Value: " + value);
} catch (Exception e) {
e.printStackTrace();
}
}
This will get you the output:Key: class java.lang.String
Value: class java.lang.Integer
这将为您提供输出:Key: class java.lang.String
Value: class java.lang.Integer
回答by NPE
Given a Map<Key,Value>
, it isn't possible to figure out Key
and Value
at runtime. This is due to type erasure(also, see Wikipedia).
给定 a Map<Key,Value>
,不可能在运行时找出Key
和Value
。这是由于类型擦除(另请参阅Wikipedia)。
It is, however, possible to examine each object (key or value) contained in the map, and call their getClass()
method. This will tell you the runtime type of that object. Note that this still won't tell you anything about the compile-type types Key
and Value
.
但是,可以检查映射中包含的每个对象(键或值),并调用它们的getClass()
方法。这将告诉您该对象的运行时类型。请注意,这仍然不会告诉您有关编译类型类型Key
和Value
.
回答by Joel
You can inspect the Class for entries in the source object by getting each element, and calling getClass on the key/value object for each. Of course, if the map wasn't genericised at source then there's no guarantee that all the keys/values in it are of the same type.
您可以通过获取每个元素并在每个元素的键/值对象上调用 getClass 来检查源对象中条目的类。当然,如果地图没有在源代码中泛化,则无法保证其中的所有键/值都属于同一类型。
回答by finnw
You cannot get the value type from the map at run-time, but maybe you can get it from the destination
array (as long as it is not null
.)
您无法在运行时从地图中获取值类型,但也许您可以从destination
数组中获取它(只要它不是null
.)
public <V> V[] convertTo(Map<?,V> source, V[] destination) {
return source.values().toArray(destination);
}
回答by tinker_fairy
In this way you can get the type of the key and value of a map
这样就可以得到一个map的key和value的类型
public class Demo
{
public static void main(String[] args)
{
Map map = new HashMap<String, Long>();
map.put("1$", new Long(10));
map.put("2$", new Long(20));
Set<?> set = map.entrySet();
Iterator<?> iterator = set.iterator();
String valueClassType="";
while (iterator.hasNext())
{
Map.Entry entry = (Entry) iterator.next();
valueClassType = entry.getValue().getClass().getSimpleName();
System.out.println("key type : "+entry.getKey().getClass().getSimpleName());
System.out.println("value type : "+valueClassType);
}
}
}