简体   繁体   中英

Java: Check if two objects of the same class have identical values

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

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.

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.

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 .

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;
    }

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM