Java:检查同一类的两个对象是否具有相同的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23385182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:20:35  来源:igfitidea点击:

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

java

提问by Peskalberto

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

提前致谢

回答by Thilo

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 equalsand hashCodemethods.

不幸的是没有。您必须编写代码才能做到这一点。如果您这样做了,请考虑将该代码放入equalshashCode方法中。

回答by Illidanek

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 equalsand hashCodemethods.

你有一个好的开始。您可以使用您的samePerson()方法来提供此功能,或者像@Thilo 所说的那样,使用equalshashCode方法。

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.

也许有一些理智的空检查和任何你需要的东西。

回答by user3588826

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.

通常你会为每个字段使用 getter。您应该在这里使用通用方法并通过对每个实例的反射调用所有 getter,然后将它们与 equals 进行比较。

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

@Edit 请参阅此处:Java 反射:如何获取 Java 类的所有 getter 方法并调用它们

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

在这里:http: //tutorials.jenkov.com/java-reflection/getters-setters.html

回答by Peter Isberg

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.

正确的答案是使用 equals() 和 hashCode() 但如果您正在寻找其他东西,您可以考虑为每个人引入一个 id- 可能是社会安全号码。那么人的public boolean isSamePersonAs(Person person)比较就可以在和比较那个 了

回答by Karibasappa G C

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.

代码如下,将其添加到您的班级中,您将获得所需的 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;
    }

回答by Aniket

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.

感谢致敬。