Working in Java 1.8, I need to determine the class of K and V in HashMap. My first attempt has not worked, and I admit I have no idea where to go from here. I am hoping to feed this
public static int foo(Object obj){
int result = 0;
String x = obj.getClass().getSimpleName();
switch(x){
case "HashMap":
Type sooper = obj.getClass().getGenericSuperclass();
Type key = ((ParameterizedType)sooper).getActualTypeArguments()[ 0 ];
Type value = ((ParameterizedType)sooper).getActualTypeArguments()[ 1 ];
System.out.println(sooper.getTypeName());
System.out.println(key.getTypeName());
System.out.println(value.getTypeName());
break;
}
}
public static void main(String[] args) {
foo(new HashMap<Integer,Character>());
}
Output:
Class :: HashMap
java.util.AbstractMap<K, V>
K
V