title: Avoid unchecked assignment in a map with multiple value types?
body: <p>I'm having trouble with a warning in Java 7:</p>
  
  <pre><code>Unchecked assignment: 'java.lang.Class' to 'java.lang.Class&lt;T&gt;'
  </code></pre>
  
  <p>I'm getting it on the line <code>Class&lt;T&gt; type = typeMap.get(key);</code> in the get function below.</p>
  
  <p>Basically what I'm trying to do here is I want to store a bunch of key/value pairs of unknown types (but all are descendants of Object with the exception of null), but not lose the type. So I created a class with the following content using generics. It has two maps (one to store the data and one to store the class type:</p>
  
  <pre><code>    private Map&lt;String, Object&gt;  dataMap = new HashMap&lt;&gt;();
      private Map&lt;String, Class&gt; typeMap = new HashMap&lt;&gt;();
  
      public  &lt;T&gt; void put(String key, T instance){
          dataMap.put(key, instance);
          if (instance == null){
              typeMap.put(key,null);
          }
          else {
              typeMap.put(key, instance.getClass());
          }
      }
  
      public &lt;T&gt; T get(String key){
          Class&lt;T&gt; type = typeMap.get(key);
          if (type == null){
              return null;
          }
          return type.cast(dataMap.get(key));
      }
  </code></pre>
  
  <p>It runs just fine, but the warning is annoying me.  Is there any way to get Java to do this cast without complaining (other than suppressing it)? Or is there a better way to accomplish what I'm trying to do?  How about in Java 8 as I haven't really had a chance to dive into it yet?</p>
  
  <p>Thanks!</p>