4

I want to know how to check if two objects of the same class have the same values ​​in each attribute.

For example:

public class Person {
String name;
String surname;
String country;
int age;    

public Person(String name, String surname, String country, int age) {
    this.name = name;
    this.surname = surname;
    this.country = country;
    this.age = age;
}

public boolean samePerson(Person person){
    //CODE
}


Person person1 = new Person("Abel", "Smith", "EEUU", 26);
Person person2 = new Person("Alexa", "Williams", "Canada", 30);
Person person3 = new Person("Abel", "Smith", "EEUU", 26);
Person person4 = new Person("Alexa", "Williams", "EEUU", 30)


person1.samePerson(person2) // return false
person1.samePerson(person3) // return true
person2.samePerson(person3) // return false
person2.samePerson(person4) // return false

The only thing I can think of is to compare the attributes one to one. Is there a simpler way?

Sorry for my english

Thanks in advance

3
  • 1
    Usually, you override the equals method from the object class, but no there is no simpler way if you consider that two persons are the same if all the values of their attributes are the same. Commented Apr 30, 2014 at 10:04
  • 6
    override equals() and hashCode() Commented Apr 30, 2014 at 10:04
  • Read this answer. It has a few different ways you can explore :) Commented Apr 30, 2014 at 10:10

6 Answers 6

6

The only thing I can think of is to compare the attributes one to one. Is there a simpler way?

Unfortunately not. You'll have to write code to do just that. And if you do, consider putting that code in equals and hashCode methods.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, it is preferred to create a equals() and hashCode() method for this case.
4

There is no simpler way. You have to implement your own way of doing this because it is a class you made yourself.

You are off to a good start. You can use your samePerson() method to provide this functionality, or, like @Thilo said, use the equals and hashCode methods.

It should go along the lines of:

public boolean samePerson(Person person){
   return this.name.equals(person.name) &&
          this.surname.equals(person.surname) &&
          this.country.equals(person.country) &&
          this.age == person.age;
}

With perhaps some sanity null checking and whatever else you require.

Comments

1

The correct answer is using equals() and hashCode() but if you are looking for something else, you could consider introducing a id for each person - maybe a social security number. Then the comparsion of persons could be implemented in the public boolean isSamePersonAs(Person person) and compare only that.

Comments

0

You need to specify how the object must be compared

and to have it compared properly implement hashcode method.

code is as below, add this in your class and you wil get desired o/p.

@Override
    public int hashCode() {
        int prime=31;
        int sum = prime*this.name.hashCode();
        sum=sum+this.surname.hashCode();
        sum=sum+this.country.hashCode();
        sum=sum+this.age;
        return sum;
    }

@Override
    public boolean samePerson(Object p) {
    if(p==null)
        return false;
    if(! (p instanceof Person))
        return false;
    Person person = (Person)p;

        return this.name.equals(person.name) && this.surname.equals(person.surname) && this.country.equals(person.country) && this.age == person.age;
    }

1 Comment

what is the use of variable prime?
-2

Folks try this one i guess it will work for you

***********This is class one "Person"*********

public class Person {
String name;
String surname;
String country;
int age;    

public Person(String name, String surname, String country, int age) {
    this.name = name;
    this.surname = surname;
    this.country = country;
    this.age = age;


  }


}

********This is main class*******

public class mainclass {

    public static void main(String arg[])
    {
        Person person1 = new Person("Abel", "Smith", "EEUU", 26);
        Person person2 = new Person("Alexa", "Williams", "Canada", 30);
        Person person3 = new Person("Abel", "Smith", "EEUU", 26);
        Person person4 = new Person("Alexa", "Williams", "EEUU", 30);
        System.out.println(samePerson(person1,person2)); 
        System.out.println(samePerson(person1,person3)); 
        System.out.println(samePerson(person2,person4)); 
        System.out.println(samePerson(person3,person4)); 
        System.out.println(samePerson(person1,person4)); 

    }
    static boolean samePerson(Person personA,Person personB)
    {    
      if(personA.name.equals(personB.name) && personA.country.equals(personB.country)&& personA.surname.equals(personB.surname )&& personA.age==personB.age )
         return true;  
      else
    return false;

   }
}

Thanks and Regards.

Comments

-3

Usually you would use getter for each field. You should use a generic way here and call all the getter via reflection on each instance, after that compare them with equals.

@Edit See here: Java Reflection: How can i get the all getter methods of a java class and invoke them

and here: http://tutorials.jenkov.com/java-reflection/getters-setters.html

3 Comments

No.. Usually, you wont do it. Reflection is not suggested here.
Ok, you would not do it yourself, but that is, what e.g. Apache Commons EqualsBuilder does via reflectionEquals - don't you think so? info at: commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/…
Even if the EqualsBuilderof the Apache Commons Lang project provides reflection methods, that does not mean that you have to use them under all circumstances. In fact, you should avoid them as long as possible. The best thing is to provide an equals method (this is the intention of this method) and write the appropriate logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.