This week's book giveaway is in the Android forum.
We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!
See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Compare different objects Java  RSS feed

 
Ranch Hand
Posts: 62
1
  • 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.
 
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: 62
1
  • 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?
 
Author and ninkuma
Marshal
Posts: 66820
168
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?
 
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: 62
1
  • 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: 62
1
  • 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..

 
Sheriff
Posts: 23714
50
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.
 
Marshal
Posts: 60748
189
  • 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!