File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Compare 2 Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Compare 2 Objects" Watch "Compare 2 Objects" New topic
Author

Compare 2 Objects

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

This my be a question but here goes. I need to compare 2 objects. So say I have a class called Employee. I create 2 instances of this class with the same values. Is there a simple way to compare the 2 Employee instances without comparing each indevidual field?

Thanks.


GenRocket - Experts at Building Test Data
Joseph Maddison
Ranch Hand

Joined: Nov 04, 2004
Posts: 53
You need to define a method equals( Object n ) in your Employee class, and whatever criteria you want to go by to determine if the two instances refer to the same employee or not, is dependent on how you define that method. Maybe SSN is enough to consider them equal, or maybe first name and last name-- it's up to you.

Once you do this, you can use the equals() method like this:



If you do not override equals( Object ), you get the one from Object, which only compares memory locations.

Hope this helps,
jdmaddison
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, when I say employee1 should equal employee2 I mean every single field. And there are about 7, which isn't much I know. But, like I said, is there no way to compare them without comparing each individual field manually? I am guessing there is not.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
by compare do you check for equals? sort?


Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Steven Bell:
by compare do you check for equals? sort?




Wouldn't that require me to implement Comparable and override those methods to compare each indivudal field myself? Seems like I am asking if it is possible without me having to write the code. Seems like this is the third time I have stated that. :roll:

I am going to just go with the answer being no. Thanks anyway.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you want a general method which works for all classes (or at least, most classes) without you having to write custom code for each class, I guess you could use somethign based on reflection. Jakarta Commons Lang has an ObjectUtils class with an equals() method that seems to do this. I"ve never tried it, but it looks promising. Something I have tried successfully is to use XStream to make a simple xml serial form of each object, and compare the strings. (May not be the fastest way to implement equals, but so far it's been very robust - and when used with JUnit's assertEquals(String, String) method, gives a nice understandable error message showing the difference between the two objects you were asserting to be equal.


"I'm not back." - Bill Harding, Twister
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Jim Yingst:
If you want a general method which works for all classes (or at least, most classes) without you having to write custom code for each class, I guess you could use somethign based on reflection. Jakarta Commons Lang has an ObjectUtils class with an equals() method that seems to do this. I"ve never tried it, but it looks promising. Something I have tried successfully is to use XStream to make a simple xml serial form of each object, and compare the strings. (May not be the fastest way to implement equals, but so far it's been very robust - and when used with JUnit's assertEquals(String, String) method, gives a nice understandable error message showing the difference between the two objects you were asserting to be equal.


Very helpful. Thanks Jim. I think the Lang package will probably do what I need.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Temper temper, ok how about something like this:
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Steven Bell:
Temper temper, ok how about something like this:


The commons lang ObjectUtils.equals doesn't really do what I needed. The code above is actually closer to what I want and I started writing almost the same thing just before you posted. Thanks, that is very helpful.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Temper temper, ok how about something like this:


sorry about the partial post. hit the wrong key.

This is a simple example. need to worry about circular reference. Didn't compile or check the code, but should be pretty close.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Man, that is so closed to working. What is messing it up is one of my fields in an Integer and so to set this I do a setId(new Integer(1)). If I do this on each object, testing objects, then it says they are different. If I don't set the ID but just set all the String values, the test says they are the same...hmmm
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
you can test
field.getType().getName() == "java.lang.Integer"
then do
Integer temp = (Integer)field.get(yourObj);
I'm not sure if there is another way. Might have to do that for all wrapper classes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compare 2 Objects