Re: Generic Collection with reference types

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



<"Ethan Strauss" <ethan dot strauss at Promega dot com>> wrote:
Overriding GetHashCode was what I was missing. That did the trick.
Do any of you have the time/desire to explain what GetHashCode is used
for? Is there anything out there that really explains the guts of
collections and how all the bits and pieces work?

The basic idea is that code is a quick way of narrowing down *possible*
matches, e.g. for keys in a hashtable.

Two keys which are equal should *definitely* have the same hashcode,
but two keys which aren't equal *might* have the same hashcode. In an
ideal world, hashcodes should be distributed such that two unequal keys
will almost always have different hashcodes.

When you look up a key in a hashtable, it first finds all the elements
with the right hashcode. After that it asks each of those whether or
not it's equal to the key you've passed in.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: why in class Boolean, hashcode() of "true" is 1231 and of "false" is 1237?
    ... The Integer hashCode() is the primitive int the Integer represents. ... That will have a pretty poor distribution of hash values, because the integer values that arise in actual running code don't tend to be completely randomly distributed. ... keys' hashCodes. ...
    (comp.lang.java.programmer)
  • how does hashCode work?
    ... java implements hashCode? ... I want to be able to take two keys and make sure that they are placed ... I figure I could just write my own hashCode and equals, ... I'd rather manipulate the standard way. ...
    (comp.lang.java.help)
  • Re: Arrays as key in a HashMap
    ... You can't use arrays literally as keys. ... hashCode for them. ... Absolute identity is probably not what you want. ...
    (comp.lang.java.programmer)
  • Re: how does hashCode work?
    ... java implements hashCode? ... I want to be able to take two keys and make sure that they are placed ... within the same 'bucket' in a hashtable. ... I figure I could just write my own hashCode and equals, ...
    (comp.lang.java.help)
  • Re: Performance of large single Hashtable vs. several Hashtables
    ... If you use many types as keys for a single hashtable, ... I will be storing objects of the same superclass called "CmsObject", ... I am, btw., not overriding the virtual GetHashCode() method in my ...
    (microsoft.public.dotnet.languages.csharp)