469,352 Members | 1,892 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Java Object comparison

Hi :

I am writing a test utility where i wish to compare two Java objects.
The object consists of many other objects. In other words the objects
tree is deep.

Does anyone one how to compare such objects. I was trying to use Java
refelction to navigate and compare.

Thanks

Dheeraj
Jul 17 '05 #1
3 10815
You could use the .compareTo method on any object to compare, and all the
objects that are contained within other objects should implement the compare
to method. I.e. all classes that contains objects should override the
compareTo method.

Hope that helped!

~ANDY~
http://www.confusedgeeks.com
"Dheeraj" <dh*******@excite.com> wrote in message
news:29**************************@posting.google.c om...
Hi :

I am writing a test utility where i wish to compare two Java objects.
The object consists of many other objects. In other words the objects
tree is deep.

Does anyone one how to compare such objects. I was trying to use Java
refelction to navigate and compare.

Thanks

Dheeraj

Jul 17 '05 #2
Implement the Camparable interface, and override the compareTo method.
Then you have to decide how to compare the objects.
fr'instance - if comparing instances of type 'Person' would you consider
salary, height, or weight to decide which is greater-than, less-than, or
equal ??? - its up to the programmer (you) to decide.

"Dheeraj" <dh*******@excite.com> wrote in message
news:29**************************@posting.google.c om...
Hi :

I am writing a test utility where i wish to compare two Java objects.
The object consists of many other objects. In other words the objects
tree is deep.

Does anyone one how to compare such objects. I was trying to use Java
refelction to navigate and compare.

Thanks

Dheeraj

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 5/22/2004
Jul 17 '05 #3
implemenet the Compare interface as per specification, thats what it's
for. taking a "short-cut" and doing it, "just this once", is not a
short-cut because it's never just this once

- perry
Dheeraj wrote:
Hi :

I am writing a test utility where i wish to compare two Java objects.
The object consists of many other objects. In other words the objects
tree is deep.

Does anyone one how to compare such objects. I was trying to use Java
refelction to navigate and compare.

Thanks

Dheeraj


Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by timothy ma and constance lee | last post: by
reply views Thread by juliane26 | last post: by
6 posts views Thread by gjain12 | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.