title: Using the comparable interface to compare two object
body: <p>I am trying to use the comparable interface so that I can use the searching algorithms in the Collection API on a list of <code>Param&lt;?,?&gt;</code> object. I don't know how to implement <code>compareTo</code> method correctly since its fields are not necessary numerical.  How could I implement this method so that the Collections API algorithms work correctly with a collection of this object?</p>
  
  <pre><code>public final class Param &lt;K,V&gt; implements Comparable&lt;Param&lt;K,V&gt;&gt;{
  
  private final K key;
  private final V value;
  
  public Param (K key, V value)
  {
      this.key = key;
      this.value = value;
  }
  
  @Override
  public boolean equals(Object obj) 
  {
      if(obj == null)
          return false;
  
      if (!obj.getClass().getName().equals(this.getClass().getName()))
          return false;
  
      @SuppressWarnings("unchecked")
      Param&lt;K, V&gt; param = (Param&lt;K, V&gt;)obj;
      if (this.getKey ().equals(param.getKey()) &amp;&amp; this.getValue().equals(param.getValue()))
          return true;
  
      return false;
  }
  
  @Override
  public int hashCode() 
  {
      int hash = 5;
  
      hash += this.key.hashCode();
      hash += this.value.hashCode();
      return hash;
  }
  
  
  public int compareTo(Param &lt;K,V&gt; o)
  {
      if (this.hashCode() == o.hashCode())
          return 0;
      return -1;
  }
  }
  </code></pre>