Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Compare different objects Java

 
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats ok. Thanks for the response. I will try to do simple compare method first .
Thanks. again..

 
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74388
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic