Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define multiple equals() function for a class

Tags:

java

I want to override "public boolean equals(Object obj)" function, for name and age, in my class named MyObject whose structure is given below

public class MyObject{
       private String name;
       private int age;
}

How can i ?

@balusC :

What about this ?

vo  = new MyObject() {
                    public boolean equals(Object obj) {
                        return ((MyObject)obj).name().equals(this.getName());

                    }


vo  = new MyObject() {
                    public boolean equals(Object obj) {
                        return ((MyObject)obj).age() == (this.getAge());
like image 738
Zeeshan Avatar asked Jun 26 '10 14:06

Zeeshan


People also ask

How do you define equals method in Java?

The Java String class equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true. The String equals() method overrides the equals() method of the Object class.

Which class is an equals method?

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y , this method returns true if and only if x and y refer to the same object ( x == y has the value true ).

How does equals () method work What does it do?

equals() Method. In Java, the String equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are the same, it returns true.


3 Answers

Your question is a bit vague, but if the sole purpose is to have different sorting algorithms depending on what property you'd like to use, then rather use a Comparator.

public class Person {
    private String name;
    private int age;

    public static Comparator COMPARE_BY_NAME = new Comparator<Person>() {
        public int compare(Person one, Person other) {
            return one.name.compareTo(other.name);
        }
    }

    public static Comparator COMPARE_BY_AGE = new Comparator<Person>() {
        public int compare(Person one, Person other) {
            return one.age > other.age ? 1
                 : one.age < other.age ? -1
                 : 0; // Maybe compare by name here? I.e. if same age, then order by name instead.
        }
    }

    // Add/generate getters/setters/equals()/hashCode()/toString()
}

which you can use as follows:

List<Person> persons = createItSomehow();

Collections.sort(persons, Person.COMPARE_BY_NAME);
System.out.println(persons); // Ordered by name.

Collections.sort(persons, Person.COMPARE_BY_AGE);
System.out.println(persons); // Ordered by age.

As to the actual equals() implementation, I'd rather let it return true when the both Person objects are techically or naturally identical. You can use either a DB-generated PK for this to compare on technical identity:

public class Person {
    private Long id;

    public boolean equals(Object object) {
        return (object instanceof Person) && (id != null) 
             ? id.equals(((Person) object).id) 
             : (object == this);
    }
}

or just compare every property to compare on natural identity:

public class Person {
    private String name;
    private int age;

    public boolean equals(Object object) {
        // Basic checks.
        if (object == this) return true;
        if (object == null || getClass() != object.getClass()) return false;

        // Property checks.
        Person other = (Person) object;
        if (name == null ? other.name != null : !name.equals(other.name)) return false;
        if (age != other.age) return false;

        // All passed.
        return true;
    }
}

Don't forget to override hashCode() as well when you override equals().

See also:

  • Object ordering
  • Sorting an ArrayList of objects
  • Overriding equals() and hashCode()
like image 188
BalusC Avatar answered Oct 05 '22 23:10

BalusC


I'm not exactly sure what you're aiming at with this. The general expectation of equals() is that it returns false for null and objects of other classes and performs value equality on the relevant fields of the class in question.

While you can certainly handle String and Integer in the following way:

public boolean equals(Object o) {
  if (o == null) return false;
  if (o instanceof String) return name.equals(o);
  if (o instanceof Integer) return ((Integer)o) == age;
  ...
}

this breaks the contract for equals so you can't do it (except not without things going wrong in very weird ways).

equals is an equivalence relation, so it has to be reflexive, symmetric and transitive. The symmetric part here is key, since if a.equals(b) then b.equals(a). Both String and Integer won't do that for you.

If you want just helper functions that check whether the name or the age is equals to a given name/age, then you can do that without using equals():

public boolean equalsName(String name) { return name.equals(this.name); }
public boolean equalsAge(int age) { return age == this.age; }
like image 34
Joey Avatar answered Oct 05 '22 23:10

Joey


Just keep it short and simple (aka KISS principle): write setters and getters. Something like in the following example:

public class Person {  
  private String name;  
  private int age;  

public String getName() {  
  return name;  
}

public int getAge() {  
  return age;  
}

And then in the method you need to do the check you can write:

Person person = new Person();
if(person.getName().equals("Something")) doThis();
if(person.getAge() == 1337) doThat();
like image 23
Igor Popov Avatar answered Oct 05 '22 22:10

Igor Popov