title: Java how to compare two generic type parameters
body: <p>The problem is to write generic method, with T constrained to extends Number. Only have a single field variable ArrayList. Have an add method that adds objects to the ArrayList, and then two methods, one that returns the largest in the ArrayList and the other the smallest. My problem is in the largest and smallest methods where I am comparing successive items in the list with each other searching for the largest and smallest, but they are of generic type T and I cannot use &lt; and &gt;, and compareTo() does not work either (I put both errors in the code provided). Can someone take a look and let me know where I am going wrong?</p>
  <pre><code>import java.util.ArrayList;
  
  public class MyList&lt;T extends Number&gt; {
     private ArrayList&lt;T&gt; list;
     
     public MyList() {
        list = new ArrayList&lt;T&gt;();
     }
     
     public void add(T obj) {
        list.add(obj);
     }
     
     public T largest() {
        boolean onFirstObj = true;
        T largestVal;
        
        for (int i = 0; i &lt; list.size(); i++) {
           if (onFirstObj) {
              largestVal = list.get(i);
              onFirstObj = false;
           }
           else {
              if (list.get(i) &gt; largestVal) {
                 largestVal = list.get(i);
              }
           }
        }
        
        return largestVal;
     }
     
     public T smallest() {
        boolean onFirstObj = true;
        T smallestVal;
        
        for (int i = 0; i &lt; list.size(); i++) {
           if (onFirstObj) {
              smallestVal = list.get(i);
              onFirstObj = false;
           }
           else {
              if (list.get(i).compareTo(smallestVal) &lt; 0) {
                 smallestVal = list.get(i);
              }
           }
        }
        
        return smallestVal;
     }
  }
  </code></pre>