• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

object comparison

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to compare two objects if they are of same type and if there contents are equal . how can this be done in java

Any help is highly appreciated.

Thanks
Rohini
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this post
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree. The post by Rob Prime tells you what you need to know. Remember that instanceof and similar only work for final classes; otherwise use the bit about this.getClass() and other.getClass(). Make sure to put the null test in first; if the other object is null then you assume it is different.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Remember that instanceof and similar only work for final classes; otherwise use the bit about this.getClass() and other.getClass().


If the itself method is final, you can use instanceof as well, as long as you hardcode the class. After all, if you compare two an object of class A with an object of a subclass of A and vice versa, both will check if the other is an instance of A. Both checks will return true, so symmetry has been achieved.
 
Shruthi Babu
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok this works fine for normal scenarios. How to compare two object arrays which in turn might contains other object arrays ..

FOR EXAMPLE

I neeed to compare if two order[] objects are equal. the order object might contain other customer object , product objects etc..

product objects might inturn contain iterm objects...

is ther a easy way to achieve this in 1.4.2,

Any help is highly appreciated
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no "1 liner" that will do it for you, but it's not _that_ difficult

What you might consider doing is looking at the 1.5 source for the Arrays and implementing something similar to the Arrays.equals(Object[] a, Object[] a2) method.



Remember, Java is open source - and if it doesn't take advantage of Generics, there's no reason you can't code up your own method. You might even copy the entire Array's class into your project.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic