This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing two Java Objects

 
Ben Zaidi
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I have a question. I want to compare two java beans of the same type. My problem is this, my bean is having
almost 100 fields, so if i start comparing the properties one by one, it will be hell lot tedious. Like we use BeanUtils.copy()
to copy the properties from one bean to the other, can we compare two beans somewhat?

I don't want to compare the properties one by one. Secondly my java bean is a simple bean, it is not implementing any
comparator interface even.

Any help or pointer in this regard will be highly helpful.

Warmly,
Ben
 
Dawn Charangat
Ranch Hand
Posts: 249
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no need to check each field... just like you have a composite primary key in databases, try identifying a few number of fields which will identify your object as unique... and companre those fields in the equals() method.
 
Ben Zaidi
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i am writing a JUnit test case and for this purpose, i need to compare the full result with the desire bean

 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use reflection, but you lose control over the order in which the fields are compared that way.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic