title: Generics and compareTo() method
body: <p>I am trying to make a SkipList and I have a method that takes a generic data type:</p>
  
  <pre><code>public void add(E key, Integer value)
  {
      Node&lt;E&gt; p; 
      p = find(key);
  }
  </code></pre>
  
  <p>Which takes you here:</p>
  
  <pre><code>public Node&lt;E&gt; find(E key)
  {
      //Start at head
      Node&lt;E&gt; p = head;
  
      while (true)
      {
          while ( (p.getRight().getKey() != Node.posInf) &amp;&amp; (p.getRight().getKey().compareTo(key) &lt;= 0 )) 
          {
              p.setRight(p.getRight());
          }
  
          //More stuff down here
      }
  }
  </code></pre>
  
  <p>The problem is on the <code>compareTo()</code> method. It says the <code>compareTo()</code> method is undefined for type <code>E</code>. In Eclipse it wants me to add two typecasts like this:</p>
  
  <p><code>((String) p.getRight().getKey().compareTo((String) key) &lt;= 0 )</code></p>
  
  <p>Why does it want <code>String</code>? The data type could be anything. I tried doing typecast of <code>E</code> instead but Eclipse wants to change it back to <code>String</code>. Any help would be appreciated.</p>