Is it possible to get the number of type parameters a class has/accepts in Java using its instance? For example,
Map<String, Integer> s = new HashMap<>();
int numParams = getNumOfTypeParameters(s); // Should return 2
I know that due to type erasure we can't get the type of these parameters but can we also not get the number of parameters?
It's as simple as
System.out.println(Map.class.getTypeParameters().length); // 2
System.out.println(Class.class.getTypeParameters().length); // 1
System.out.println(Object.class.getTypeParameters().length); // 0
Just look at the documentation for java.lang.Class
and you'l find this.