Splitter
Splitter

Reputation: 307

Overriding CompareTo using Equal method

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.

What I want to know is it possible to override CompareTo using an Equal method. If so how can I check it in main.

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.

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 bit tangled pardon me for that was just trying different approaches.

Upvotes: 4

Views: 5869

Answers (3)

ratchet freak
ratchet freak

Reputation: 48196

first off using == to check for equality of objects should never be done; replace it with

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

for compareTo you can simply return the compareTo of the strings

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

Upvotes: 0

RonK
RonK

Reputation: 9652

The contract for compareTo according to JavaDoc indicates this:

The implementor must also ensure that the relation is transitive: (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 is naturally not true nor transitive.

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

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

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

Upvotes: 5

Ted Hopp
Ted Hopp

Reputation: 234795

I would strongly recommend against this approach. The compareTo method is expected to return an integer that indicates the relative ordering of the objects. The contract specifies (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 non-zero integers of opposite sign. Your implementation does not do that.

Upvotes: 2

Related Questions