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

Peskalberto
  • 61
  • 1
  • 1
  • 4
  • 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. – Alexis C. Apr 30 '14 at 10:04
  • 6
    override equals() and hashCode() – TheLostMind Apr 30 '14 at 10:04
  • [Read this answer](http://stackoverflow.com/a/20093642). It has a few different ways you can explore :) – Rahul Apr 30 '14 at 10:10

6 Answers6

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.

Thilo
  • 257,207
  • 101
  • 511
  • 656
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.

Illidanek
  • 996
  • 1
  • 18
  • 32
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.

Peter
  • 5,556
  • 3
  • 23
  • 38
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;
    }
Karibasappa G C
  • 2,686
  • 1
  • 18
  • 27
-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.

Aniket
  • 118
  • 7
-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

Community
  • 1
  • 1
  • 2
    No.. Usually, you wont do it. Reflection is not suggested here. – TheLostMind Apr 30 '14 at 10:11
  • 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: http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/builder/EqualsBuilder.html – user3588826 Apr 30 '14 at 10:26
  • 1
    Even if the `EqualsBuilder`of 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. – Seelenvirtuose Apr 30 '14 at 10:54