0

I'm building a map for caching purposes that contains a class as the key and the constructors of the class (Array) as the value.

How do I declare this map using generics?

private static Map<Class<T>, java.lang.reflect.Constructor<T>[]> classConstructorsCache = new HashMap<>();

Above statements gives a "T cannot be resolved to a type" error.

Bernie Lenz
  • 1,967
  • 23
  • 45
  • The short answer is you can't. You'll have to use a wildcard type (`>`) and cast as necessary. Note that `Class.getConstructors()` returns `Constructor>[]` because [generic arrays are unsafe](https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays). – shmosel Jan 04 '17 at 03:44
  • Note that in a lot of cases, you’re likely want to use [`ClassValue`](https://docs.oracle.com/javase/8/docs/api/?java/lang/ClassValue.html) instead of a `Map` for this kind of association. – Holger Jan 06 '17 at 12:05

0 Answers0