Does anyone know about a free open source library (utility class) which allows you to compare two instances of one Java bean and return a list/array of properties which values are different in those two instances? Please post a small sample.
Cheers
Tomas
BeanComparator
can be used for collection sorting.It is a
Comparator
implementation that compares beans based on a shared property value.There is no need to write a
PersonComparator
with lots of property options to sort by . Instead, a singleBeanComparator
class takes care of it all.You will find a full example here, with the following scenario:
BeanComparator of Apache commons is what you are looking for.
Update. A simple example that compares JavaBeans with one property (comparation is made agains only one property, you should create as many BeanComparators as properties you want to match).
After some tests one result is successful. This is the output:
Obviously increasing TestBean array size will increase the chances to get matches.
You need to import to your project the following jars: commons-logging-version.jar, commons-beanutils-version.jar, commons-beanutils-core-version.jar, commons-beanutils-bean-collections-version.jar, commons-collections-version.jar.
The files are included inside commons-logging, commons-beanutils and commons-collections APIs.