• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compare 2 Objects

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joseph Maddison
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by compare do you check for equals? sort?


 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Temper temper, ok how about something like this:
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic