title: In Java is it possible to create a type-safe Map of classes to instances of their class?
body: <p>I would like to create a map that uses a class as a key to return an instance of that class. Something like:</p>
  
  <pre><code>&lt;T&gt; Map&lt; Class&lt;T&gt;, T &gt; instanceMap = new HashMap&lt; Class&lt;T&gt;, T &gt; ();
  instanceMap.put( Boolean.class, Boolean.TRUE );
  instanceMap.put( String.class, "asdf" );   
  instanceMap.put( Integer.class, 11 );
  
  Boolean b = instanceMap.get( Boolean.class );
  Integer i = instanceMap.get( Integer.class );
  String s  = instanceMap.get( String.class  );
  </code></pre>
  
  <p>Is this possible? I have a feeling that no it is not because I cannot indicate that <code>T</code> is meant to be a generic type rather than a class named "T". It is feels somehow like "higher-order generics".</p>
  
  <p><strong>EDIT:</strong> I know I could try to extend Map and implement my own wrapper etc, but I am specifically asking about doing this just using using Java's generic support. I am more interested in the idea rather than this particular case.</p>
  
  <p><strong>EDIT 2:</strong> As pointed out below, this is a duplicate (or more specifically a subcase) of the question: <a href="https://stackoverflow.com/questions/416540/java-map-with-values-limited-by-keys-type-parameter">Java map with values limited by key&#39;s type parameter</a> . Had I been able to find wording as eloquent as that, I would have likely found that answer and not posted this.</p>