Search code examples
javadeepequals

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. What is the fastest and efficient way to check Deep Equal for these java objects?

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.


Solution

  • Override equals() or have a *helper method (bad option!) and do it in 5 steps :

    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