aspose file tools*
The moose likes Java in General and the fly likes problem with class comparison: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem with class comparison:" Watch "problem with class comparison:" New topic
Author

problem with class comparison:

Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

problem with class comparison:
I am currently writing a program that consists of taking 2 data sets ( one from a switch dump and the other from the database ) and identifying which data from set1 is not in set2 and vice versa. Each class ( basically a data structure ) is as follows:

and

to test if a SwitchLine is equal to a DatabaseLine, the following must be true:
esn = esn
telno = telno
SwitchLine.option is one of MANY DatabaseLine.features ( example option "CFW" can be one of ( "100", "400", "115" )
or "CFB" can be one of ( "100", "400", "600" ) )
to test if a DatabaseLine is equal to a Switchline, the reverse is true:
esn = esn
telno = telno
DatabaseLine.feature can be one of MANY SwitchLine.options ( example feature "100" can be one of ( "CFB", "CFW" ) as illustrated above )
I've tried to implement the equals method that worked when I called DatabaseLine.equals( SwitchLine ) and vice versa. The problem arose when I added all the switch lines to a HashSet and tried to utilize the HashSet.contains( DatabaseLIne ), it did not invoke my overridden .equals() method ( which worked when explicitly called ) but used the Object's default .equals() method
To summarize, the pseudo code is something like:
create a set of database data
create a set of switch data
generate a list of switch data that does not have an equal match in the database data
generate a list of database data that does not have an equal match in the switch data
Any suggestions on how to implement the equals ( I've got the hashCode already done as well ) method would be appreciated,
Jamie
[ July 29, 2003: Message edited by: Jamie Robertson ]
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

this was my original SwitchLine class equals method which worked, somewhat.


Here is the way I implemented checking for a valid combination of features/options:


this is the problem class comparison code:

AM I just using the HashSet wrong???
[ July 29, 2003: Message edited by: Jamie Robertson ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Does HashSet even use equals()? I thought it just used hashCode(). Are you sure that your hashCode() is compatible with your equals() methods?


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Michael Morris:
Does HashSet even use equals()? I thought it just used hashCode(). Are you sure that your hashCode() is compatible with your equals() methods?
it uses hashcode to approximate the location in the set for fast retrieval, but eventually uses equals to check for equality:
from the docs(contains method): "Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e))."
Jamie
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Duh, it would have to if there were more than one object in the same bucket. But why is it not using the correct equals() method. That doesn't seem possible. Have you put some System.out.printlns in the equals() method to see if it is being invoked when you add to the HashSet?
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Michael Morris:
Duh, it would have to if there were more than one object in the same bucket. But why is it not using the correct equals() method. That doesn't seem possible. Have you put some System.out.printlns in the equals() method to see if it is being invoked when you add to the HashSet?

Yeah, unfortunately I have, and nothing was printed. I'll have to make sure there is no classpath/compilation problems that are throwing it out of wack( precedence in the classpath to an older version and all that funky stuff! ). But I'm off for the day, thanks for the help, I'm sure I'll need more again tomorrow!
Jamie
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

problem solved here
Thanx,
Jamie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with class comparison:
 
Similar Threads
Ranch roundup game question
problem with String buffer
Whizlabs question... their explanations seems weird to me
why we need to override equals and hascode