473,414 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Hashtable: GetHashCode uniqueness

Hi,
I'm just concerned about storing a large amount of items in a Hashtable. It
seems to me that as the number of keys in the Hashtable increases, so does
the chance of key clashes.

Does anyone know about the performance with regards to this?

Cheers
Greg
Nov 22 '05 #1
4 4086
I don't have a direct answer to your question, but if you want to provide
your own algorithm for generating hash values, have a look at the
IHashCodeProvider interface.

I guess the chance of Key clashes depends on what you are using as a key
(string, object, integer etc?) and how the hash codes are generated. Sorry I
can't be of more help.

Trev.

"Greg Bacchus" <FB**********@spammotel.com> wrote in message
news:eW**************@TK2MSFTNGP12.phx.gbl...
Hi,
I'm just concerned about storing a large amount of items in a Hashtable. It seems to me that as the number of keys in the Hashtable increases, so does
the chance of key clashes.

Does anyone know about the performance with regards to this?

Cheers
Greg

Nov 22 '05 #2
Hi,
A Hashtable is the most efficient form of retrieval structure as long as
you have an efficient hash algorithm (seeded random number generator).
and the performance degradation with volume is negligible so long as the
system is using some form of extendable hashing.
Look up info on extensible hashing to see how it all works.
An example link is here:
http://feast.ucsd.edu/CSE232W99/Indexing/sld034.htm
although the 'good' hash algorithm he suggests is a poor one :)
The GetHashCode() in .Net uses a version of the Zobel/Ramakrishna algorithm
(the best performing algorithm I'm aware of) but has been adapted to
produce a guaranteed unique key (not necessary for a hash table but
sometimes required internally by .Net).
If you want to 'roll your own' for some obscure data structure then I've
attached an adaptation of the hash function below.

Cheers,
Jason
Public Function computeHashCode(ByVal StrToHash As String) As Integer
Dim c As Byte() =
System.Text.Encoding.UTF8().GetBytes(StrToHash.ToC harArray())
Dim h As Integer = 31 'seed chosen at random
Dim i As Integer
For i = 0 To c.Length - 1
' L=5, R=2 works well for ASCII input
h = (h Xor ((h * 32) + (h \ 4) + c(i))) And &HFFFF
Next
Return h
End Function
On Tue, 27 Jan 2004 09:48:26 +1300, Greg Bacchus wrote:
Hi,
I'm just concerned about storing a large amount of items in a Hashtable. It
seems to me that as the number of keys in the Hashtable increases, so does
the chance of key clashes.

Does anyone know about the performance with regards to this?

Cheers
Greg

Nov 22 '05 #3
Sorry, just looked at the doco and the default key from GetHashCode is
*not* guaranteed unique, but I found a page in MSDN describing the issues:
http://msdn.microsoft.com/library/de...hcodetopic.asp

Cheers,
Jason

On Tue, 27 Jan 2004 08:55:22 +1100, Jason Sobell wrote:
Hi,
A Hashtable is the most efficient form of retrieval structure as long as
you have an efficient hash algorithm (seeded random number generator).
and the performance degradation with volume is negligible so long as the
system is using some form of extendable hashing.
Look up info on extensible hashing to see how it all works.
An example link is here:
http://feast.ucsd.edu/CSE232W99/Indexing/sld034.htm
although the 'good' hash algorithm he suggests is a poor one :)
The GetHashCode() in .Net uses a version of the Zobel/Ramakrishna algorithm
(the best performing algorithm I'm aware of) but has been adapted to
produce a guaranteed unique key (not necessary for a hash table but
sometimes required internally by .Net).
If you want to 'roll your own' for some obscure data structure then I've
attached an adaptation of the hash function below.

Cheers,
Jason
Public Function computeHashCode(ByVal StrToHash As String) As Integer
Dim c As Byte() =
System.Text.Encoding.UTF8().GetBytes(StrToHash.ToC harArray())
Dim h As Integer = 31 'seed chosen at random
Dim i As Integer
For i = 0 To c.Length - 1
' L=5, R=2 works well for ASCII input
h = (h Xor ((h * 32) + (h \ 4) + c(i))) And &HFFFF
Next
Return h
End Function

Nov 22 '05 #4
There are two places where you can get collisions:

1) Two different objects can return the same number from GetHashCode()
2) Two different objects return different numbers from GetHashCode(), but
when become the same number when the two numbers are modded with the hash
table size.

The first one is an inherent feature of GetHashCode(), and depends mostly on
how good the hash code function is. With a function with good uniformity (ie
values spread over the entire int range), this shouldn't be a big issue.

The second one depends on the size of the hash table. You can change the
packing factor on the hashtable class to make the table bigger (and
therefore lookup faster), at the cost of more memory used.

In my experience, the hashtable class is pretty fast given a decent hash
function.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Greg Bacchus" <FB**********@spammotel.com> wrote in message
news:eW**************@TK2MSFTNGP12.phx.gbl...
Hi,
I'm just concerned about storing a large amount of items in a Hashtable. It seems to me that as the number of keys in the Hashtable increases, so does
the chance of key clashes.

Does anyone know about the performance with regards to this?

Cheers
Greg

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Greg Bacchus | last post by:
Hi, I'm just concerned about storing a large amount of items in a Hashtable. It seems to me that as the number of keys in the Hashtable increases, so does the chance of key clashes. Does anyone...
4
by: C Villalba | last post by:
I am wondering if there is any problems with using a structure (contains 2 strings and 1 datetime) as a key to a hashtable. I have created one without overriding the gethashcode or equals...
3
by: Ronin | last post by:
Hi i am using following code which extracts information from XML file and creates an instance of class which it adds to hash table. problem is i am unable to extract information from hashtable :...
33
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are...
8
by: Matthias Kientz | last post by:
I have a class which should override the GetHashcode() function, which look like this: public class A { public string str1; public string str2; //other methods...
8
by: Robin Tucker | last post by:
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a database with (hashed on key) and a file view (hashed...
4
by: Bob | last post by:
If two attribute instances of the same type have all their fields values identical, the compiler will say that they are not equal to each other, yet adding them both as keys in a hashtable will...
4
by: Joseph Bergevin | last post by:
Why can't I use an int for a key? Doing so evidently doesn't result in unique IDs being generated. I can convert the array into a delimited string, which works fine, but then I have a good deal of...
8
by: Ashish Khandelwal | last post by:
-----See below code, string str = "blair"; string strValue = "ABC"; string str1 = "brainlessness"; string strValue1 = "XYZ"; int hash = str.GetHashCode() ; // Returns 175803953 int hash1 =...
28
by: Tony Johansson | last post by:
Hello! I can't figure out what point it is to use GetHashCode. I know that this GetHashCode is used for obtaining a unique integer value. Can somebody give me an example that prove the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.