如果相同类别的对象是相同的。

huangapple go评论65阅读模式
英文:

If objects of same class are identical

问题

我有一个有15个数据成员的Java类。
我有该类的2个对象。
我想要检查这两个对象是否相同。
逐个比较每个数据成员很耗时间。
有更好的方法吗?

英文:

I have a java class which has 15 data members.
I have 2 objects of that class.
I want to check if the 2 objects are identical or not.
Comparing each data member is time consuming.
Is there a better approach

答案1

得分: 1

如果你的意思是“它需要大量的CPU时间” - 不,实际上并不需要。它只是比较了一大堆位,而且那确实是你想要做的,所以,就这么做:逐个比较每个字段。

如果你的意思是“那会产生很多编写工作”,集成开发环境(IDEs)可以自动生成那些代码,不过之后你就得查看它,维护它,在你添加一个字段时忘记更新那些样板代码时会遇到麻烦。因此,你可以使用Project Lombok@Value或者@EqualsAndHashCode功能。

英文:

If you mean 'it takes a lot of CPU time' - no it doesn't. It just compares a whole bunch of bits and that's.. literally exactly what you want to do, so, just do that: Compare each and every field.

If you mean 'that takes a lot of writing', IDEs can autogenerate that stuff, though then you have to look at it, maintain it, get in trouble when you add a field and forget to update those buckets of boilerplate. So, you could instead use Project Lombok's @Value or @EqualsAndHashCode feature.

答案2

得分: 0

// 你可以重写 `equals` 方法,以便在同一类型的两个实例之间编写自定义比较器。有关更多信息,请参阅这个 Spring [文档](https://www.baeldung.com/java-comparing-objects)。此外,还可以参考 `rzwitserloot` 的答案,使用 Lombok 注解来自动生成此 equals 方法。

public class MyClass {
  private String dataField1;
  private String dataField2;
  // 声明剩余的 13 个实例级数据字段

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    MyClass that = (MyClass) o;

    return dataField1.equals(that.dataField1) &&
          dataField2.equals(that.dataField2);
    // 比较剩余的 13 个实例字段
  }
}

然后,您可以使用这个 equals 方法来比较两个实例:

MyClass m1 = new MyClass();
MyClass m2 = new MyClass();

System.out.println(m1.equals(m2));
英文:

You can override the equals method in order to write your own custom comparator between two instances of the same type. Refer to this Spring documentation for more info. Also, refer to rzwitserloot's answer for simply using Lombok annotations to have this equals method automatically generated for you.

public class MyClass {
  private String dataField1;
  private String dataField2;
  // declare remaining 13 instance level data fields

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
	if (o == null || getClass() != o.getClass()) return false;
	MyClass that = (MyClass) o;

	return dataField1.equals(that.dataField1) &&
	      dataField2.equals(that.dataField2);
    // Compare remaining 13 instance fields
  }
}

Then you can compare the two instances by using this equals method:

MyClass m1 = new MyClass();
MyClass m2 = new MyClass();

System.out.println(m1.equals(m2));

答案3

得分: 0

从你的评论中,我理解每个对象将与其存储的版本进行一次比较。在这种用例中,没有比将字段进行比较更好的方法。

一个浮现在人们脑海中的想法是使用从字段值派生的哈希码。如果哈希码不同,字段必须也不同。而这只需要进行一次整数比较。如果哈希码相同,您仍然需要检查字段值,因为可能会出现碰撞:不同的对象导致相同的哈希码。

但我想,无论在哪个步骤中,相当大比例的对象都没有改变其值。因此,引入哈希码只能改善“已更改”情况,而计算一个哈希码的时间可能比比较两个对象要长。

英文:

From your comment

> Suppose i have 1000 objects with unique id stored in one table.same
> 1000 objects comes again via some scheduler and it checks whether the
> values have been changed or not.

I understand that each object will be compared exactly once against its stored version. In this use case, there's no better way than comparing the fields.

An idea that comes to one's mind is to use hash codes derived from the field values. If hash codes are different, the fields must be different too. And that is done by just one integer comparison. If the hash codes are identical, you still have to check the field values, as there can be collisions: different objects resulting in the same hash code.

But I suppose, in any step quite a big percentage of your objects have not changed their values. So, introducing hash codes can only improve the "changed" cases, and computing one hash codes will probably take longer than comparing two objects.

huangapple
  • 本文由 发表于 2020年10月7日 12:41:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64237325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定