Forums Register Login

problem with class comparison:

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Does HashSet even use equals()? I thought it just used hashCode(). Are you sure that your hashCode() is compatible with your equals() methods?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
problem solved here
Thanx,
Jamie
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 977 times.
Similar Threads
Whizlabs question... their explanations seems weird to me
problem with String buffer
why we need to override equals and hascode
Ranch roundup game question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:49:41.