Hi All,

I am pretty new to Java and i am having difficulty using overloading on the equals and compareTo methods. I have a sample application for a school and i need to check for duplicates in records using the equals and compareTo methods.

All records have Firstname, Lastname and EntryYear and i need to using equals find out if firstname, lastname and entryyear match the passed in object. In compareTo i need to compare by firstname and then lastname so i can sort an array.

All help greatly appreciated, if you need the code please let me know.

Recommended Answers

All 7 Replies

Assuming that you are using java 1.5 or later then:

public class SomeObject implements Comparable<SomeObject> {
    

     public boolean equals(Object obj) {
         if (obj==null) return false;
         if (obj instanceof SomeObject) {
               SomeObject so = (SomeObject)obj;
               // write your code here that compares the values of the so attributes with the attributes of this class.
         }
         return false;
     }

     public int compareTo(SomeObject o) {
          // look at the API of the Comparable interface. It explains how this method should be implemented.
     }
}

Hi Again,

Sorry for the hassle, the equals worked after i filled in the blanks and tweaked a bit thanks. I have looked up the Comparable API but i genuinely can't understand a word of it, could you please help me create a compareTo method based on two strings, First sorting by the first string and then by the second string. Thanks in advance for the help

Kind Regards,
Sinister747

The basics of Comparable really come down to returning a negative value if "this" is less than the object passed, a positive value if "this" is greater, or zero if they're equal

class SomeClass implements Comparable<SomeClass> {
    public int compareTo(SomeClass other) {
        // If this < other, return -1 
        // If this > other, return 1
        // If they're equal, return zero.
        return 0;
    }
}

Now all you need to do is code in the "if" statements to compare them.

The basics of Comparable really come down to returning a negative value if "this" is less than the object passed, a positive value if "this" is greater, or zero if they're equal

class SomeClass implements Comparable<SomeClass> {
    public int compareTo(SomeClass other) {
        // If this < other, return -1 
        // If this > other, return 1
        // If they're equal, return zero.
        return 0;
    }
}

Now all you need to do is code in the "if" statements to compare them.

Thanks for the help, but if you don't mind how exactly can i do a greater or less with Strings? i would presume alphabetically but is that really veasible?

Thanks for the help, but if you don't mind how exactly can i do a greater or less with Strings? i would presume alphabetically but is that really veasible?

inside compareTO you should use compareTo again which will return 1/0/-1, you shouldnt do < but compareTo ... for example.

public int compareTo(Object obj){
Employee emp = (Employee) obj;
if(this.firstName.compareTo(emp.firstName)!=0)
return this.firstName.compareTo(emp.firstName);
}else if(this.middleName.compareTo(emp.middleName)!=0){
return this.middleName.compareTo(emp.middleName);
}else if(this.lastName.compareTo(emp.lastName)!=0){
return this.lastName.compareTo(emp.lastName);
}else
return 0;

if firstname !=0 then it will return 1/-1 and if they are equal it will chk for middle name, if middle name is not equal it will return 1/-1
else it will chk for last name

Since vchandra has already answered your question, I would like to add that any class that implements the Comparable interface can be sorted using the compareTo method. If you look at the API the String class implements the Comparable, so it has a "compareTo" method that you can call.

Thanks everyone, that got it all sorted

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.