title: Java Generics: How to specify a Class type for a generic typed class?
body: <p>I have a POJO specified as: <code>MyClass&lt;U&gt;</code>, where <code>U</code> is the generic type parameter.
  I am trying to write a utility method which accepts a class reference <code>Class&lt;T&gt;</code> and populates a map of type <code>Map&lt;String, T&gt;</code> (accepts the map to populate).</p>
  
  <p>This method is implemented like:</p>
  
  <pre><code>static void populateMap(Map&lt;String, T&gt; map, Class&lt;T&gt; type) {
  
      ...
  
      // Parses into the specified type and returns an object of that type.
      T obj = parse(..., type);
      map.put (key, obj);
      ...
  
      return map;
  }
  </code></pre>
  
  <p>This compiles fine. In my caller, I attempt to populate a map with any <code>MyClass</code> instance (irrespective of type) as the value. Hence I use the following code:</p>
  
  <pre><code>// Loses type information
  Map&lt;String, MyClass&lt;?&gt;&gt; m = new HashMap&lt;&gt;();
  populateMap(m, MyClass.class);
  </code></pre>
  
  <p>This does not compile. Compilation error:</p>
  
  <blockquote>
    <p>The method <code>populate(Map&lt;String,T&gt;, Class&lt;T&gt;)</code> in the type ... is not applicable for the arguments <code>(Map&lt;String,MyClass&lt;?&gt;&gt;, Class&lt;MyClass&gt;)</code></p>
  </blockquote>
  
  <p>How can I fix this?</p>