java java比较对象:使用反射?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1520941/
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-10-29 16:54:10  来源:igfitidea点击:

java compare objects: using reflection?

javareflectionobjectcompare

提问by codeModuler

I have an object which itself has multiple objects as fields. The question I have is, I have two objects of these kind and I want to compare these two. I know I can do equals, comparator etc. but is there a way to use reflection to get the properties of the object and make comparison.

我有一个对象,它本身有多个对象作为字段。我的问题是,我有两个这样的对象,我想比较这两个对象。我知道我可以做等于、比较等,但是有没有办法使用反射来获取对象的属性并进行比较。

for example, if I have a Car object, which as wheels object, which has tires object, which has bolts object. Please remember all the above objects are individual and not nested classes. How do I compare 2 car objects?

例如,如果我有一个 Car 对象,它作为轮子对象,它有轮胎对象,它有螺栓对象。请记住上述所有对象都是单独的,而不是嵌套的类。如何比较 2 个汽车对象?

Any help is appreciated?

任何帮助表示赞赏?

Thanks

谢谢

回答by skaffman

Apache Commons Langhas an EqualsBuilderclass that does exactly this (see the reflectionEquals()methods)

Apache Commons Lang有一个EqualsBuilder类可以做到这一点(参见reflectionEquals()方法)

 public boolean equals(Object obj) {
   return EqualsBuilder.reflectionEquals(this, obj);
 }

EqualsBuilderalso provides more explicit methods for null-safe comparison of specific fields, which makes writing "proper" (i.e. non-reflective) equals methods a bit less onerous.

EqualsBuilder还为特定字段的空安全比较提供了更明确的方法,这使得编写“正确的”(即非反射)方法变得不那么繁琐。

回答by cletus

public class Car {
  private Wheels wheels;
  // other properties

  public boolean equals(Object ob) {
    if (!(ob instanceof Car)) return false;
    Car other = (Car)ob;
    // compare properties
    if (!wheels.equals(other.wheels)) return false;
    return true;
  }
}

is the correct approach. Automatic comparison via reflection is not recommended. For one thing "state" is a more generic concept than reflected property comparison.

是正确的做法。不推荐通过反射进行自动比较。一方面,“状态”是一个比反射属性比较更通用的概念。

You could write something that did deep reflection comparison but it's kinda missing the point.

你可以写一些进行深度反射比较的东西,但它有点错过了重点。

回答by p3t0r

Most modern IDE's have generators for hashcode and equals which let you select the properties to take into account. Those beat performance of their reflective counterparts easily.

大多数现代 IDE 都有 hashcode 和 equals 生成器,可以让您选择要考虑的属性。那些很容易击败他们的反思同行的表现。

回答by Jesper

The idea is interesting, but please be aware that reflection can be slow. If you need to do a lot of comparisons, or you are putting your objects in collection classes that do comparisons (for example HashMap, HashSetetc.) then comparing objects via reflection can become a performance bottleneck.

这个想法很有趣,但请注意反射可能很慢。如果你需要做大量的比较,或者你把你的对象集合类是做的比较(例如HashMapHashSet等),然后通过反射比较对象可以成为性能瓶颈。