Re: hash and Collections. A solution for some.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: hash and Collections. A solution for some.

Ralph Boland
I haven't followed this thread closely so I apologize in advance if I
am somewhat off topic but I think what I have to say may be of
interest to some of you.  I think the current method of determining a
hash value for a collection is fine in the general case
and if it is inadequate for your particular needs then implement your
own collection class that does what you want.

This was the case for one of my applications so I created classes
HashArray and HashSet.  Both of these classes used as their hash value
the XOR of the the hash values of the collection elements (which was
of course stored in an instance variable).  Each time an element was
added, removed, or replaced in the collection the hash value of the
collection had to be recomputed (required O(1) time).
Of course this approach breaks down if the collection elements can
change their hash values.

My solution is not ideal for the hashArray case because two hashArrays
containing the same elements but in a different order would have the
same hash value.  But this didn't matter for my application because I
used hashArrays as a set converted into an array (with values sorted)
for time/space efficiency reasons.  This worked really well for me, in
part because the elements stored in the hashArrays and hashSets also
had really good hash values.

Also note that, in my application, two hashArrays or hashSets
containing the same elements were considered to be equal.

If anyone makes use of this idea I wouldn't mind a quick email on your
experience.

Ralph Boland