Re: HashTable
George wrote:
I am using a HashTable with HashSets as the keys (they point to
integers and it has to be that way round because of an algorithm I
am implementing).
I calculate the HashSet from a different method and then I use
hashtablename.containsKey(hashset). The problem is that altough the
values of the hashset are identical containsKey will fail.
Any ideas as to how I might get the correct results?
Gordon Beaton wrote:
Have you modified the HashSet in any way (added or removed elements)
after storing it in the HashTable?
Have you confirmed that the HashSet in the HashTable and the object
you use to find it with, in fact have the same hashCode()?
Assuming java.util.HashSet, the hashCode() calculation is completely reliable.
Are they equals()?
Ay, there's the rub. They aren't.
What kind of objects are stored in the HashSet, and did you remember
to implement hashCode() and equals() methods properly in *those*
objects?
Irrelevant. He's comparing HashSets, not their contents.
It's hard to be sure without an SSCCE, but a couple of things might be wrong:
First of all, who provides the HashTable class? Maybe there is something
strange about it. (Unless you meant java.utilHashtable, but your spelling
says otherwise. If you did mean Hashtable, why are you using that Map
implementation? Is synchronization needed?)
Second, what is the definition of HashSet.equals()? As it happens, you're
guaranteeing that the HashSet in the HashTable (sic) is different from the one
you're querying. That's why the lookup is failing.
--
Lew