Overriding CompareTo Using the Equal Method

I am trying to read in a company vector and returns true if two companies have the same name, false otherwise, which I did using the ComapareTo method. In my method, which will call the "equals" method in the Company class. My "equals" method should override the corresponding method in the Object class.

What I want to know is that you can override CompareTo using the Equal method. If so, how can I test it basically.

The purpose of this program is to check 2 companies in a vector, if so return true else false. Here is the code that I stuck mostly to test it.

public class Company implements Comparable <Company> { /** * @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<String> v = new Vector<String>(); 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); } 

The code is a bit confused, I apologize for just trying to use different approaches.

+4
source share
3 answers

The contract on compareTo according to the JavaDoc points to this:

The developer must also ensure that the relation is transient : (x.compareTo (y)> 0 &> y.compareTo (z)> 0) implies x.compareTo (z)> 0.

So the answer to your question is no, you cannot. In your case: compare("A","B") == 1 and compare("B","A") == 1 , which means that "A"<"B" and "B"<"A" which, of course, is not true and not transitive.

If you want to save time, implement compareTo and use it inside equals . Something like that:

 public boolean equals(Object o) { // ... Some stuff you need to complete here first return this.compareTo(o) == 0; } 

Naturally compareTo expects an object of type Company - so you will need to verify that before sending it to compareTo

+4
source

I would highly recommend against this approach. The compareTo method is expected to return an integer indicating the relative ordering of the objects. The contract determines (in part) that a.compareTo(b) returns 0 exactly when a.equals(b) returns true . However, when a.equals(b) returns false , then a.compareTo(b) and b.compareTo(a) should return nonzero integers with the opposite sign. Your implementation does not do this.

+2
source

first use == to verify equality of objects should never be done; replace it with

 if(this.cName.equals(b.cName)){ 

for comparison. You can just return compareTo rows

 public int compareTo(Company b) { return this.cName.compareTo(b.cName); } 
0
source

All Articles