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? 6 2356
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.H ashAlgorithm - 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.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.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.H ashAlgorithm - 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.co m>
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...
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.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
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.comschre ef in bericht
news:12******** *******@news.vm ware.amis.hr...
>
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.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.H ashAlgorithm - 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.co m> 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...
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.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.co m>
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?
On Jun 27, 7:12*am, "buu" <a...@a.comwrot e:
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 possible.
I also log previous values in arrays, so data size can't be ignored.
The data is such that I can implement value semantics by implementing
IComparable.CompareTo and overriding all the comparison operators as
well as Object.Equals(object...
|
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 override int GetHashCode()
int hash = 0
for (int i = 0; i < data.Length; i++
|
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
**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
|
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;
otherwise, Hashtable might not work correctly."
Does any one know, why we must also override Equals,
Please give my an example:)
Thanks,
Stoyan
|
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 database,
indexing the field, and going from there. I don't know exactly how to
do this, so I was hoping for some pointers!
Another possibility is to encrypt the string using MD5, which would
tend to shorten it, but MD5 doesn't produce a number-only...
| |
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
unique. How can we fix this problem?
--
Mike
|
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****
|
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 currently using Java to
do this but would like to move the source to C#. Java's String class has
a method that hashes strings. I was wondering if C# has a method which
does the same?
In my Java version of the program I am using the Multiply Add and...
|
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?
|
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |