473,326 Members | 2,081 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,326 software developers and data experts.

something better than String.getHashCode

buu
I'm using getHashCode sub from String object, but sometimes it gives me
duplicate values for different strings.
Difference is always when there are some numbers in strings.

Is there any better/new getHash code function or any way to do some better
hashing?
Jun 27 '08 #1
6 2302
buu <ah*@a.comwrote:
I'm using getHashCode sub from String object, but sometimes it gives me
duplicate values for different strings.
Yes, it will.
Difference is always when there are some numbers in strings.

Is there any better/new getHash code function or any way to do some better
hashing?
Hashing will always give duplicate values when there are more potential
"source" values than "hash" values (as there clearly are in the case of
strings hashing to any fixed size hash).

If you want to use a hash for integrity checking etc you should use the
classes derived from System.Security.Cryptography.HashAlgorithm - but
if it's just for a hash table or similar structure, then GetHashCode
should be absolutely fine.

What's your actual use case?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
buu

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
buu <ah*@a.comwrote:
>I'm using getHashCode sub from String object, but sometimes it gives me
duplicate values for different strings.

Yes, it will.
>Difference is always when there are some numbers in strings.

Is there any better/new getHash code function or any way to do some
better
hashing?

Hashing will always give duplicate values when there are more potential
"source" values than "hash" values (as there clearly are in the case of
strings hashing to any fixed size hash).

If you want to use a hash for integrity checking etc you should use the
classes derived from System.Security.Cryptography.HashAlgorithm - but
if it's just for a hash table or similar structure, then GetHashCode
should be absolutely fine.

What's your actual use case?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
I have a collection of Strings (names & addresses together) wich I found to
be best to search at the way to create a hashCode of that string and to use
as a key in a dictionary.
It's good, because it's fast, but it gives me duplicate hashCodes
sometimes...
Jun 27 '08 #3
buu <ah*@a.comwrote:
I have a collection of Strings (names & addresses together) wich I found to
be best to search at the way to create a hashCode of that string and to use
as a key in a dictionary.
It's good, because it's fast, but it gives me duplicate hashCodes
sometimes...
You should use the string itself as the key in the dictionary. That's
the whole point of dictionaries! The dictionary is going to take the
hash code and use that itself (without assuming it's going to be
unique) - why do you feel the need to take a hash yourself to start
with?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #4
Buu,

I don't know if you use this in a kind of batch process, otherwise remember
that any interaction with a user will const much more time then you can win
with any optimalizing of methods you describe now. A user will almost for
sure not see the by you gained time by this.

Cor

"buu" <ah*@a.comschreef in bericht
news:12***************@news.vmware.amis.hr...
>
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
>buu <ah*@a.comwrote:
>>I'm using getHashCode sub from String object, but sometimes it gives me
duplicate values for different strings.

Yes, it will.
>>Difference is always when there are some numbers in strings.

Is there any better/new getHash code function or any way to do some
better
hashing?

Hashing will always give duplicate values when there are more potential
"source" values than "hash" values (as there clearly are in the case of
strings hashing to any fixed size hash).

If you want to use a hash for integrity checking etc you should use the
classes derived from System.Security.Cryptography.HashAlgorithm - but
if it's just for a hash table or similar structure, then GetHashCode
should be absolutely fine.

What's your actual use case?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com

I have a collection of Strings (names & addresses together) wich I found
to be best to search at the way to create a hashCode of that string and to
use as a key in a dictionary.
It's good, because it's fast, but it gives me duplicate hashCodes
sometimes...

Jun 27 '08 #5
buu

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
buu <ah*@a.comwrote:
>I have a collection of Strings (names & addresses together) wich I found
to
be best to search at the way to create a hashCode of that string and to
use
as a key in a dictionary.
It's good, because it's fast, but it gives me duplicate hashCodes
sometimes...

You should use the string itself as the key in the dictionary. That's
the whole point of dictionaries! The dictionary is going to take the
hash code and use that itself (without assuming it's going to be
unique) - why do you feel the need to take a hash yourself to start
with?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
You're right...
but, what if I want to check if some name & address exist, and by that
'hash-key' there are some entries in dictionary wich are same by the key,
but not same by the content?
Jun 27 '08 #6
On Jun 27, 7:12*am, "buu" <a...@a.comwrote:
You're right...
but, what if I want to check if some name & address exist, and by that
'hash-key' there are some entries in dictionary wich are same by the key,
but not same by the content?
Then you need to have a dictionary which goes the other way. Basically
you'll need a dictionary for each kind of key you want to look things
up by.

Jon
Jun 27 '08 #7

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

Similar topics

2
by: Fuzzy | last post by:
I have defined a struct in my application that contains 3 integers that maintain state information. I have a lot of these structs in time-critical portions of my app, so they must be as fast as...
7
by: Avin Patel | last post by:
Hi I have question for GetHashCode() function, Is it correct in following code or there is more efficient way to implement GetHashCode() function class IntArray public int data public...
3
by: cmrchs | last post by:
Hi, why is it requested that when Equals() is implemented in a class that GethashCode() must be implemented as well ? thnx Chris ...
5
by: Stoyan | last post by:
Hi All, I don't understand very well this part of MSDN: "Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code;...
3
by: writebrent | last post by:
For database purposes, I need an easier /faster way of comparing two strings for equivalency. I have the idea of somehow converting the string to a unique number, storing that number in the...
6
by: Mike9900 | last post by:
Hello, I am wondering why GetHashCode() for the string is using two differemt algorithms in .NET Framework 1.1 and 2. This is creating a big problem, because we relied on this hashcode as...
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 =...
6
by: j1mb0jay | last post by:
I am currently working on a dictionary populating program. I currently have a socket connection my local news server and am trawling through all of the articles looking for new words. I am...
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.