1

Suppose I have two objects of same class for example:

public class Example {
    String name;
    String rollNo;
    String address;
    String phoneNo;
    String city;
}

Example obj1 = new Example();
    obj1.name = "Name";
    obj1.rollNo = "10";
    obj1.address = "Address";
    obj1.phoneNo = "Phone Number";
    obj1.city = "City";

Example obj2 = new Example();
    obj2.name = "Name";
    obj2.rollNo = "10";
    obj2.address = "Address";
    obj2.phoneNo = "Phone Number";
    obj2.city = "City";

Here I want to compare obj1 with obj2 the catch is I don't want to do this with if condition i.e. getting each member variable of obj1 then comparing it with obj2 variable.

equals method of java.lang.Object class will not work here as it compares object reference.

My question is, is there any Java API which will compare two objects and its member variables.

Vishrant
  • 15,456
  • 11
  • 71
  • 120
  • you have to override equals in your example class. – kai Jan 22 '14 at 10:42
  • You can override the equals method. – Kevin Bowersox Jan 22 '14 at 10:42
  • Thanks for quick reply, but overriding will not solve my problem. Actually I am looking for Java API which will do this. – Vishrant Jan 22 '14 at 10:45
  • 'overriding will not solve my problem'. Why not? What do you need from an API that can't be done by overriding `equals`? – PakkuDon Jan 22 '14 at 10:46
  • Actually @PakkuDon if I override `equals` method then I will have to write if conditions, what if there are 20 member variable in a class then I will have to write 20 `if` condition, actually I want to avoid `if` conditions. – Vishrant Jan 22 '14 at 10:50
  • You could let eclipse auto generate it or you read [this](http://stackoverflow.com/questions/6959307/java-automatic-equals-and-hashcode). – Sam Jan 22 '14 at 10:52

2 Answers2

3
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Example other = (Example) obj;
        if (address == null) {
            if (other.address != null)
                return false;
        } else if (!address.equals(other.address))
            return false;
        if (city == null) {
            if (other.city != null)
                return false;
        } else if (!city.equals(other.city))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (phoneNo == null) {
            if (other.phoneNo != null)
                return false;
        } else if (!phoneNo.equals(other.phoneNo))
            return false;
        if (rollNo == null) {
            if (other.rollNo != null)
                return false;
        } else if (!rollNo.equals(other.rollNo))
            return false;
        return true;
    }

Paste this equals function into your exmaple class and then compare the objects like this:

 if(obj1.equals(obj2)) {  //will return true now

 }
kai
  • 6,702
  • 22
  • 38
  • Thanks Kai, actually problem statement remains same, I am looking for some Java API, suppose my member variable increases then I will have to write more `if` condition, that actually I want to avoid. – Vishrant Jan 22 '14 at 10:47
  • there is no api. you cant avoid an if condition for every single member. there is only an api which updates the equals method when you add an new member. – kai Jan 22 '14 at 10:51
1

In Java, there is three alternatives to compare objects :

  1. By overriding the Object.equals() method.
  2. Using the Generic java.util.Comparator Interface
  3. Using the java.lang.Comparable interface

Check this question, and this link for more details.

Community
  • 1
  • 1
Naili
  • 1,564
  • 3
  • 20
  • 27