473,806 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4110
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
IHashCodeProvid er 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**********@s pammotel.com> wrote in message
news:eW******** ******@TK2MSFTN GP12.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.Enc oding.UTF8().Ge tBytes(StrToHas h.ToCharArray() )
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.Enc oding.UTF8().Ge tBytes(StrToHas h.ToCharArray() )
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**********@s pammotel.com> wrote in message
news:eW******** ******@TK2MSFTN GP12.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
429
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 know about the performance with regards to this? Cheers Greg
4
1414
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 functions and it seems to work. However i am not sure if occasionally this can cause a problem with uniqueness. The examples i have seen all use classes and override the two functions mentioned above. I may make the change from structure to class, but i...
3
2493
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 : here is the piece of code public void readEmailRecords() XmlTextReader reader = new XmlTextReader("C:\\Mailer\\EmailRec.xml") reader.MoveToElement(); try
33
3326
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 used before adding, removing or iterating the Hashtable. The program runs in a high workload environment. After running a few days, now it suddenly catchs this Exception when inserting a pair of key and object, stacktrace =...
8
9913
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
1692
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 on string). So, for example, when I want to know if the file "xyz.abc" is in the cache, I can write myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I can write myTable.ContainsKey ( 123 ). Or do the keys all have to be of...
4
1063
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 throw an exception. Is this expected behavior or a bug? I've included an example below. Run the code as is and no exception is thrown. Run it with both attributes contructed identically, or remove all fields in the TestAttribute class, and an...
4
3023
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 overhead parsing/casting the ints back. I'd obviously use a (jagged)multidimensional array of my Value type instead of a key:value setup, but the int sets aren't tightly grouped, so I'd have a lot (maybe 85%) of empty slots in the array. Still,...
8
5870
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 = str1.GetHashCode(); // Returns 175803953 Hashtable ht = new Hashtable(); ht.Add(hash ,strValue); ht.Add(hash1,strValue1); // ****ERROR****
28
3790
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 usefulness of this GetHashCode or it it of no use at all? A mean that if I get an integer from current time in some way what should I use it for?
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9187
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.