Equivalance of two objects

205 views Asked by At

Is there any library, given two objects of any java class would precisely say whether they are equal or not? (the class may not have hashCode defined)

else is there any simple way to implement this?

7

There are 7 answers

0
thomas.scheuchzer On

You can use EqualsBuilder from commons-lang. http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/builder/EqualsBuilder.html#reflectionEquals%28java.lang.Object,%20java.lang.Object%29

The reflectionEquals method will check every field in the object against an other object for equality. It's working with reflection. If you don't implement equals() then it simply checks the references. Implementing equals() and hashCode() is still advisable.

0
sri91 On

I was looking for something similar to this

http://code.google.com/p/deep-equals/

This will check for semantic equality between objects using value equivalence if the hash codes and equals aren't defined.

0
Suresh Atta On

IMHO , There are no libraries to check that two Objects are equal(Since that equal should define by you).

You need to override equals method and implement your logic inside.

@Override
public boolean equals(Object other){
    //your logic
}

the class may not have hashCode defined

Beware of that statement, when you use/deal with Collections you'll end up with surprise results ,since you are not overriding hashcode method.

Be careful with equals and override contract

Whenever a.equals(b), then a.hashCode() must be same as b.hashCode().

0
Ruchira Gayan Ranaweera On

You have to override equals() and hashCode() method.

You don't want to worry about this. you can write your object class and generate these method using IDE.

0
Aniket Thakur On

You need to override equals() and hashCode() method of your custom class.

Default implementation of equals() internally just does == comparison. It means it will just check if both the references you are comparing are pointing to same object or not. If you want to also compare actual data in it you have no option than to override it to add your comparison logic.

0
Thomas On

You could use Apache Commons' EqualsBuilder which provides a reflectionEquals(a,b) method.

But in most cases it would still be best to implement equals and hashCode yourself (note that by contract you should always override both).

Note that most collections depend on equals and hashCode to be correctly implemented but if you have a common super class, you might be able to override then there like this:

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

public int hashCode() {
 return HashCodeBuilder.reflectionHashCode(this);
}

NOTE: please be aware that reflection adds some performance overhead so heavy use of those methods might not be wise.

0
Peter Lawrey On

You can use XMLEncoder and see if the two objects encode the same way. This assumes you have getters and setters instead. Having a proper equals() isn't so hard given you can generate it with your IDE.