Java generics why this does not work?

Why can't I create a map with the following generics?

Map<Class<K extends Item>, K> classMap;
+3
source share
2 answers

Since it is Mapalready generated, your job when creating the link is to populate the type parameter. If it is not inside the method parameterized with the help K, the compiler does not know what to replace K(and if it was inside the parameterized method, you could not have K extends Itemin the body Keither already extends Itemor not).

Parameters of a new type can come in the signature of classes and methods with an implicit promise that they will be filled later. They cannot enter the declaration.

+7
source

Map, . K .

+1

Source: https://habr.com/ru/post/1731738/


All Articles