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:
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 ]
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
Joined: Jan 30, 2002
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?
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