Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - something better than String.getHashCode

Reply
 
Old 06-26-2008   #1 (permalink)
buu


 
 

something better than String.getHashCode

On Jun 27, 7:12*am, "buu" <a...@xxxxxx> wrote:
Quote:

> 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

My System SpecsSystem Spec
Old 06-26-2008   #2 (permalink)
Jon Skeet [C# MVP]


 
 

Re: something better than String.getHashCode

buu <aha@xxxxxx> wrote:
Quote:

> I'm using getHashCode sub from String object, but sometimes it gives me
> duplicate values for different strings.
Yes, it will.
Quote:

> 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 - <skeet@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
My System SpecsSystem Spec
Old 06-26-2008   #3 (permalink)
buu


 
 

Re: something better than String.getHashCode


"Jon Skeet [C# MVP]" <skeet@xxxxxx> wrote in message
news:MPG.22ce010d61403b4edc2@xxxxxx
Quote:

> buu <aha@xxxxxx> wrote:
Quote:

>> I'm using getHashCode sub from String object, but sometimes it gives me
>> duplicate values for different strings.
>
> Yes, it will.
>
Quote:

>> 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 - <skeet@xxxxxx>
> 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...


My System SpecsSystem Spec
Old 06-26-2008   #4 (permalink)
Jon Skeet [C# MVP]


 
 

Re: something better than String.getHashCode

buu <aha@xxxxxx> wrote:
Quote:

> 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 - <skeet@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
My System SpecsSystem Spec
Old 06-27-2008   #5 (permalink)
Cor Ligthert[MVP]


 
 

Re: something better than String.getHashCode

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" <aha@xxxxxx> schreef in bericht
news:1214511058.992007@xxxxxx
Quote:

>
> "Jon Skeet [C# MVP]" <skeet@xxxxxx> wrote in message
> news:MPG.22ce010d61403b4edc2@xxxxxx
Quote:

>> buu <aha@xxxxxx> wrote:
Quote:

>>> I'm using getHashCode sub from String object, but sometimes it gives me
>>> duplicate values for different strings.
>>
>> Yes, it will.
>>
Quote:

>>> 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 - <skeet@xxxxxx>
>> 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...
>
>
My System SpecsSystem Spec
Old 06-27-2008   #6 (permalink)
buu


 
 

Re: something better than String.getHashCode


"Jon Skeet [C# MVP]" <skeet@xxxxxx> wrote in message
news:MPG.22ce13419c129527dc4@xxxxxx
Quote:

> buu <aha@xxxxxx> wrote:
Quote:

>> 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 - <skeet@xxxxxx>
> 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?


My System SpecsSystem Spec
Old 06-27-2008   #7 (permalink)
Jon Skeet [C# MVP]


 
 

Re: something better than String.getHashCode

On Jun 27, 7:12*am, "buu" <a...@xxxxxx> wrote:
Quote:

> 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
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
object.GetHashCode()? .NET General
Find a string within a variable string PowerShell
problems with $var | select-string -pattern $string -q PowerShell
How export-csv deals with string versus string[] PowerShell
String PRODUCT_NAME was not found in string table Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46