title: How to avoid unchecked cast warnings with Java Generics
body: <p>Somehow my old question was closed, so I open a new one:</p>
  
  <p>I am using Java Generics to implement a generic bidirectional Hash Map out of an SQL Query. It should be able to map any combination of String, Integer pairs back and forth. It should be used like this:</p>
  
  <pre><code>String sql = "SELECT string_val, int_val FROM map_table";
  PickMap&lt;String, Integer&gt; pm1 = new PickMap&lt;String, Integer&gt;(sql);
  
  String key1 = "seven";
  Integer value1 = pm1.getLeft2Right(key1);
  
  Integer key2 = 7;
  String value2 = pm1.getRightToLeft(key2);
  </code></pre>
  
  <p>Of course it should be possible to create an pm (Integer, Integer) and so on...</p>
  
  <p>My implementation of Pick Map looks like this (without the getter...):</p>
  
  <pre><code>public class PickMap&lt;L, R&gt; {
  
      private final HashMap&lt;L, R&gt; left2Right = new HashMap&lt;L, R&gt;();
      private final HashMap&lt;R, L&gt; right2Left = new HashMap&lt;R, L&gt;();
  
      public PickMap(String sql) throws OException {
          DTable d = new DTable(sql);
          int colTypeL = d.t.getColType(1);
          int colTypeR = d.t.getColType(2);
          Extractor&lt;L&gt; extLeft  = (Extractor&lt;L&gt;) getInstance(colTypeL);
          Extractor&lt;R&gt; extRight = (Extractor&lt;R&gt;) getInstance(colTypeR);    
          int numRows = d.t.getNumRows();
          for(int i=1;i&lt;=numRows;i++) {
              L leftVal = extLeft.extract(d, i);
              R rightVal = extRight.extract(d, i);
              this.left2Right.put(leftVal, rightVal);
              this.right2Left.put(rightVal, leftVal);
          }
      }
  
      private Extractor&lt;?&gt; getInstance(int type) {
          if(type == 1)
              return new IntExtractor();
          else
              return new StringExtractor();
      }
  }
  
  interface Extractor&lt;E&gt; {
      E extract(DTable source, int row);
  }
  
  class IntExtractor implements Extractor&lt;Integer&gt; {
  
      @Override
      public Integer extract(DTable source, int row) {
          int value = 5;
          return new Integer(value);
      }
  }
  
  class StringExtractor implements Extractor&lt;String&gt; {
  
      @Override
      public String extract(DTable source, int row) {
          String retVal = "hello";
          return retVal;
      }
  }
  </code></pre>
  
  <p>I have no compiler errors and I'm pretty sure, that it will work this way. BUT I'm getting unchecked cast warnings on the "getInstance" methods Where I cast Extractor(E) to Extractor(L)...</p>
  
  <p>How should I cast properly? Or what am I missing? Or should I just suppress those warnings? </p>