简体   繁体   English

检查两个java对象的Deep Equal的最快捷有效的方法是什么?

[英]What is the fastest and efficient way to check Deep Equal for two java objects?

I have got two java objects with byte[] field of the size of the order of millions . 我有两个java对象,其byte[]字段的大小为数百万 What is the fastest and efficient way to check Deep Equal for these java objects? 检查这些java对象的Deep Equal的最快捷有效的方法是什么?

Sample Entity: 样本实体:

@Entity
public class NormalBook
{

  @Id
  private String bookId;

  @Column
  private String title;

  @Column
  private byte[] pdfFile;

  //setters and getters

  }

Note: I am doing it for an ORM tool basically I am checking an object ( which is in managed state ) with an object present in Persistence Context. 注意:我正在为ORM工具执行此操作,基本上我正在使用Persistence Context中存在的对象检查对象( 处于托管状态 )。

Override equals() or have a *helper method (bad option!) and do it in 5 steps : 覆盖equals()或者有一个* helper方法(错误选项!)并分五步完成:

1. Check for *not null*.
2. Check for same *type*.
3. Check for *size of byte[]*.
4. Check for `==` (*reference equality* of byte[]) 
5. Start comparing byte values 

Use the following in the definition of equals() on your object's class: 在对象类的equals()定义中使用以下内容:

java.util.Arrays.equals(bs1, bs2)

You might also want to check if they are the same array (instance) first. 您可能还想先检查它们是否是相同的数组(实例)。 Though this method may well do that anyway. 虽然这种方法无论如何都可能做到这一点。

For example (and making some assumptions about your class that contains the arrays): 例如(并对包含数组的类做出一些假设):

public boolean equals(Object obj) {
    if(this == obj)
        return true;
    if(!(obj instanceof MyObject)) // covers case where obj null, too.
        return false;
    return Arrays.equals(this.bytes, ((MyObject)obj).bytes);
}

If there are other fields in your class, your equals() should take those into account too. 如果您的班级中还有其他字段,则您的equals()应考虑这些字段。

(There might be a better answer to the question if you could provide more information about what kind of data is stored in the arrays.) (如果您可以提供有关阵列中存储的数据类型的更多信息,则可能有更好的答案。)

if your class have fields like byte[] you can use something like: 如果你的类有像byte[]这样的字段你可以使用类似的东西:

public class MyClass {


    byte[] a;

    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MyClass other = (MyClass) obj;
        if (!Arrays.equals(a, other.a))
            return false;
        return true;
    }


}

If you are concern about performance and can assure a unique hascode (this is important hascode need to be unique ) then you can just compare the hascode. 如果您关注性能并且可以确保唯一的hascode (这是重要的hascode必须是unique ),那么您可以只比较hascode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 java 中的 hashmap 上检查正则表达式模式列表的最有效和最快的方法是什么 - What is the efficient and fastest way to check a list of regex pattern on a hashmap in java 在java中连接两个大(超过1.5GB)文件的最有效(最快)方法是什么? - What is the most efficient (fastest) way to concatenate two large (over 1.5GB) files in java? 检查Java中两个对象是否完全相同 - Check if two objects are completely equal in Java java中如何判断两个对象是否相等? - How to check if two objects are equal in java? 在 Java 中比较两个集合的最快方法是什么? - What is the fastest way to compare two sets in Java? 检查数组中所有元素是否相等的最快方法 - fastest way to check if all elements in an array are equal 在 Java 对象之间复制数据的最快方法是什么? - What would be the fastest way of copying data between Java objects? Java-如何检查来自同一抽象类的两个对象是否相等 - Java - How to check if two objects, from the same abstract class, are equal 在Java中将两个位集相交到新的BitSet中最快的方法是什么? - What would be the fastest way to intersect two bitsets into a new BitSet in Java? 两个Java对象的深度比较 - Deep Comparision of Two Java Objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM