Hashtable/Dictionary collisions

后端 未结 5 1169
孤街浪徒
孤街浪徒 2021-01-06 11:39

Using the standard English letters and underscore only, how many characters can be used at a maximum without causing a potential collision in a hashtable/dictionary.

5条回答
  •  失恋的感觉
    2021-01-06 12:08

    There's no guarantee that you won't get a collision between single letters.

    You probably won't, but the algorithm used in string.GetHashCode isn't specified, and could change. (In particular it changed between .NET 1.1 and .NET 2.0, which burned people who assumed it wouldn't change.)

    Note that hash code collisions won't stop well-designed hashtables from working - you should still be able to get the right values out, it'll just potentially need to check more than one key using equality if they've got the same hash code.

    Any dictionary which relies on hash codes being unique is missing important information about hash codes, IMO :) (Unless it's operating under very specific conditions where it absolutely knows they'll be unique, i.e. it's using a perfect hash function.)

提交回复
热议问题