Ive been searching for a robust way to compare my objects, I came across the ObjectUtils , and had the immidiate doubt that would it be able to compare it efficiently or not as I do not know how it works internally and documentation on apache org site about this is scarce.
Can someone please help me with this??
EDIT:
When I say compare , all I really need to to compare for equality of MYOBJ, where MYOBJ is a custom object I have defined , which has various variables in it(all these vars are primitive data types like int,long,float String which can be compares in a straightforward manner if they are not null), but this might change in the future.
I was not sure would BeanUtils.areEqual method be able to handle such a case and what if I include my own datatypes(non -primitives) inside this MYOBJ.
Thanks
Your Question is very vague, I don’t really know what you are talking about, so I’ll go in several Directions.
“compare my Objects” can mean several things. In Java, this usually means comparing them for sorting, i.e. through the
Comparable/Comparatorinterfaces. WhileObjectUtilsdoes provide a null-safe compare method, it won’t help you beyond that. What you need is either a customComparatoror for your objects to implementComparable.CompareToBuildercan help you with both, to a certain extent:If, on the other hand, you want to compare the properties of different object types, then you are looking in the totally wrong direction. Have a look at Commons / BeanUtils instead. Sample code:
And if you just want to implement equals() correctly, look at
EqualsBuilder: