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.
If the two objects have the same values, equals() will return true . In the second comparison, equals() checks to see whether the passed object is null, or if it's typed as a different class. If it's a different class then the objects are not equal. Finally, equals() compares the objects' fields.
The == operator can't compare conflicting objects, so at that time the compiler surrenders the compile-time error. The equals() method can compare conflicting objects utilizing the equals() method and returns “false”.
reflect. Field is used to compare two field objects. This method compares two field objects and returns true if both objects are equal otherwise false. The two Field objects are considered equal if and only if when they were declared by the same class and have the same name and type.
In Java, the == operator compares that two references are identical or not. Whereas the equals() method compares two objects. Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With