title: Overriding CompareTo using Equal method
body: <p>I am trying to read in a vector of companies and returns true if two companies have the same name, false otherwise Which I have done using ComapareTo method. In my method that will call the “equals” method in the class Company. My “equals” method must override the corresponding method in the class Object.</p>
  
  <p>What I want to know is it possible to override CompareTo using an Equal method. If so how can I check it in main.</p>
  
  <p>The objective of this program is to check the 2 companies in a vector if so return true else false. Here is the code I am stuck in main to test it.</p>
  
  <pre><code>public class Company implements Comparable &lt;Company&gt; {
  
  /**
   * @param args
   */
  private String cName;
  
  
  
  public String getName()
  {
      return cName;
  }
  
  public int compareTo(Company b)
  {
      if(this.cName == b.cName)
      {
          System.out.println(" from compareTo true");
          return 1;
      }
  
      else
      {
          System.out.println(" from compareTo false");
          return 0;
      }
  }
  
  public boolean equal(Object o)
  {
      if (o instanceof Company)
      {
          Company c = (Company) o;
          if(this.cName.equals(c.cName))
          {
              System.out.println(" from equal true");
                  return true;
          }
      }
      System.out.println(" from equal false");
      return false;
  }
  
  public static void main(String[] args) {
      // TODO Auto-generated method stub
  
      Vector&lt;String&gt; v = new Vector&lt;String&gt;();
  
      Company obj1 = new Company();
      Company obj2 = new Company();
  
      v.add("Rio tinto");
      v.add("BHP");
      v.add("BHP");
      v.add("CBA");
  
      Collections.sort(v);
  
      System.out.println(v);
  
  }
  </code></pre>
  
  <p>The code is bit tangled pardon me for that was just trying different approaches. </p>