• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compare different objects Java  RSS feed

 
Jeena Jeen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I have a question regarding object comparison. I have to different kind of objects where one is serialized and other is not. And i want to see if they are same or not. How can i do that?




Most likely there content are same because they have same fields. I can't override equals in any of the classes as they are already provided and not allowed to change them. So i was wondering if anyone have any idea how to compare them.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a method to compare both, but you would never be able to compare using equals.
 
Jeena Jeen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yaah i i can't compare with equals. but what kind of method do i need to create? should i make a method that compare each ield. but that will be too expensive. can i use MD5 or something like that for this compare. I m not intersted in what is different. i just want to know if they are exactly same or not?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeena Jeen wrote:should i make a method that compare each ield [sic]. but that will be too expensive.

Really? What metrics have you gathered to support this supposition?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeena Jeen wrote:yaah i i can't compare with equals. but what kind of method do i need to create? should i make a method that compare each ield. but that will be too expensive. can i use MD5 or something like that for this compare. I m not intersted in what is different. i just want to know if they are exactly same or not?

You really think that comparing three fields - two of them primitives - will be slower than calculating an MD5 algorithm? Just write a simple comparison method.
 
Jeena Jeen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No actually its not just three primitive. These are big classes with more fields in it.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeena Jeen wrote:No actually its not just three primitive. These are big classes with more fields in it.

Ah, sorry. I took your example too literally.

I'd still go for the simple solution first, though, and only consider anything else if I can prove that's too slow.
 
Jeena Jeen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats ok. Thanks for the response. I will try to do simple compare method first .
Thanks. again..

 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the classes are the same except for one being serializable, I would just take the code from the equals() method of one of them and put that into a new method which compares a ClassA and a ClassB instead of comparing two ClassA's, let's say.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I quite agree. you should write a canEqual() method, but you will have to be very careful about typing. I would suggest a static method, in a utility class.I agree with those who say it will not be expensive to compare field to field, particularly if those fields have equals() methods which are well optimised. I also agree that an MD5 would be more expensive, and will not guarantee equality.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!