473,890 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about Equals and GetHashCode

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?

One more question should these Equals and GetHashCode be used in pair?

//Tony

Jun 27 '08
28 3797
On Jun 12, 6:51*pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
You could still multiply the 0 by 37 in the first field incorporated, *
preserving the symmetry aspect. *I just don't see the point in starting *
with 17 versus 0. *It has the sense of "magic" about it, but unlike the *
multiplication by 37, I don't see any practical advantage. *A less *
"magical" initializer of 0 would be just as good.

I think. *:)

Pete
It does seem like magic doesn't it? But, then again mathematics is
full intriguing quirks so it wouldn't surprise me if there was a valid
reason.

It reminds of me of the section in Knuth Vol 2 where he talks about
random number generators and warns that using one random number
generator to seed another generator may actually make the end result
*less* random! I know...complete ly off topic. My mind works in
mysterious ways :)

Jun 27 '08 #21
On Thu, 12 Jun 2008 19:41:32 -0700, Brian Gideon <br*********@ya hoo.com>
wrote:
It does seem like magic doesn't it? But, then again mathematics is
full intriguing quirks so it wouldn't surprise me if there was a valid
reason.
Well, in this case it's pretty much just algebra. We're not in the realm
of "intriguing quirks of mathematics". It's proveable that the
initializer always winds up just affecting the hash code by a fixed
constant. It's hard to see how that fixed constant could be significant.
At best, it would just shift all of the hashed codes by the same amount
along the bins for the hash table. It won't change their relative
arrangement, and thus wouldn't affect the actual operation.

Pete
Jun 27 '08 #22
On Jun 13, 12:51 am, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
No, my point is that you could just initialize the local hash variable to
0, rather than 17.

You could still multiply the 0 by 37 in the first field incorporated,
preserving the symmetry aspect. I just don't see the point in starting
with 17 versus 0. It has the sense of "magic" about it, but unlike the
multiplication by 37, I don't see any practical advantage. A less
"magical" initializer of 0 would be just as good.

I think. :)
Okay, I'll have to consult Effective Java, which is where the advice
originally came from. I believe there's some benefit to having two
magic numbers which are coprime to each other. Wikipedia doesn't show
this as one of the example hash functions, unfortunately, so there's
no discussion there to help :(

Jon
Jun 27 '08 #23
On Thu, 12 Jun 2008 22:54:40 -0700, Jon Skeet [C# MVP] <sk***@pobox.co m>
wrote:
Okay, I'll have to consult Effective Java, which is where the advice
originally came from.
For what it's worth, the last discussion I was in when this came up,
Bloch's book was also quoted. So at least I'm seeing a consistent source
here. :)
I believe there's some benefit to having two
magic numbers which are coprime to each other.
As I mentioned, my inspection of the problem concluded that no matter what
number you pick for the initial value, it factors out to a single constant
added to the final result. Straight algebraic manipulation. And since
adding a single constant to a hash code isn't going to affect its
usefulness, selection of some specific initial value should not either.

I don't have a copy of "Effective Java", but if you do and can go back to
look to see if he offered any sort of justification for the initializer, I
suppose I'd be interested in hearing that. I've been wrong before, but
usually there's someone around who can explain how or why. Or, I suppose,
maybe that's not true and I've actually been wrong a lot more than I or
anyone else realized. :) But assuming the latter isn't actually the
case, I remain relatively confident that I've figured this one correctly.
Until proven otherwise, of course. :)

Pete
Jun 27 '08 #24
On Jun 13, 3:28 am, Brian Gideon <briangid...@ya hoo.comwrote:
To be perfectly honest, I'm not sure how important it is for the hash
code to be absolutely random as opposed to just good.
I think to know that we'd have to know exactly what you mean by
"random". Completely evenly distributed?

I suspect it's beneficial because whatever the hashtable does to
process hashcodes, if you have some pattern/clustering to your data
you're more likely to get a poor distribution in terms of buckets.
However, I would hope that a good hashtable implementation would make
it *unlikely* that you'd see a problem. Then again, my understanding
of the *exact* implementation of hashtables is somewhat lacking.
I think that would be an interesting topic for a blog. I do try to
read your blog posts so I'll catch it if you do decide to post on the
topic.
I'll put it on my mental list of future posts :)

Jon
Jun 27 '08 #25
Re adding a seed; actually, it has one advantage... a lot of default
instances get created just with new Foo() or
Activator.Creat eInstance()
Without a seed, this type of hash algorithm (using + and *) will
return 0 for every default instance. Which is fine in itself, since
every default instance is broadly comparable... but! Now imagine that
we are comparing tuples of values, or lists of values - where the
tuple's hashcode is a composite (again using + and *) of the contained
values' hashcodes.
It is nice (from a purely theoretical standpoint) to be able to
immediately distinguish a set of 2 (default instances) from a set of 8
(default instances) - or the sets {A,B} and {[default],A,B}.

OK; in reality, this is an unlikely scenario - but it does still seem
desirable to avoid any "obvious" causes of zero. Yes, even with a seed
you will still get zero from some values, but then as a genuine
exception, and so it is not an issue.

Re random - "uniform" would be a better term; Int32.GetHashCo de() is
uniform but not random.

Marc
Jun 27 '08 #26
On Thu, 12 Jun 2008 23:36:38 -0700, Marc Gravell <ma**********@g mail.com>
wrote:
[...]
It is nice (from a purely theoretical standpoint) to be able to
immediately distinguish a set of 2 (default instances) from a set of 8
(default instances) - or the sets {A,B} and {[default],A,B}.
Um, okay. I'll buy that. Still, it seems like a pretty esoteric scenario
to drive a hash code implementation. Especially since it still doesn't
address sets of identical lengths. :)
[...]
Re random - "uniform" would be a better term; Int32.GetHashCo de() is
uniform but not random.
Agreed. I was going to pick that nit myself, but then I didn't. :)

Pete
Jun 27 '08 #27
Um, okay. I'll buy that. Still, it seems like a pretty esoteric scenario
to drive a hash code implementation. Especially since it still doesn't
address sets of identical lengths. :)
Re identical lengths - if the lengths and contents match, I'm happy
for the hashcode to match under that approach, since they sound like
the same set (assuming the definition of equality ties with
GetHashCode(), which it should).

Marc
Jun 27 '08 #28
On Jun 13, 12:54*am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
Okay, I'll have to consult Effective Java, which is where the advice
originally came from. I believe there's some benefit to having two
magic numbers which are coprime to each other. Wikipedia doesn't show
this as one of the example hash functions, unfortunately, so there's
no discussion there to help :(

Jon
Okay, I'm looking through Knuth Vol 3 (those who have read the books
know that it isn't an easy read) on hashing and he talks about
choosing a prime number as the multiplier for the "scrambling
function" (aka GetHashCode). However, I don't see anything about the
initial seed being a prime number.

My best guess right now is definitely along the lines of what Marc
suggested. It has more to do with the end result you get from
combining the values of more than one call to GetHashCode (via
combinations of + and *).

From clues in Knuth this may be related to random number generation
theory. Since the scrambling function needs to be random it's easy to
see how that works in. Seeding the hash code with a number that is
coprime to the multiplier may be an attempt to prevent unwanted
periods from developing in the final hash code when combinging results
from multiple calls to GetHashCode.
Jun 27 '08 #29

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

Similar topics

1
3232
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't understand is:
3
17654
by: Fei Li | last post by:
Hi, take string class as an example, who can explain the difference? Thanks
3
5664
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...
2
9589
by: Mark | last post by:
I've built a class and overroad the Equals method. I've gotten the warning below. I'm unfamiliar with the "GetHashCode" method. An explantion of the method and its relation to the warning would be appreciated. warning: 'myClass' overrides Object.Equals(object o) but does not override Object.GetHashCode() Thanks in advance. Mark
2
2673
by: Dot net work | last post by:
Hello, My simple code is here: Public Class MyDictionary Inherits System.Collections.DictionaryBase Private Class MyElement Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean End Function
8
3757
by: Kenneth Baltrinic | last post by:
When one overrides the Equals() method of an object, one is supposed to override GetHashCode() as well and this makes good sense. But I have seen lots of people who do this and do not override the == and != opperators. Am I missing something or when would one want to have different implementations for Equals and ==? --Ken
12
2229
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid overloading of == and != but instead translate each call of objA==objB automatically in System.Object.Equals(objA, objB). This would remove inconsistencies like myString1==myString2
1
1970
by: eramfelt | last post by:
Why doesnt ArrayList implements the Equals() or GetHashCode() methods? How is the developer supposed to check if an array list is equal to another array list? Is the developer supposed to iterate through the array list to find out if they are equal or not? This should be handled by the framework classes and I don't want to do any unnecessary implementation (read copy-paste-problems). If I look through the other classes in...
5
2396
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member object of generic type T, that supports IEquatable<T>, and a method, DoComparisons(T obj) to compare the member object to the object passed in.
0
9979
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
9826
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
11234
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, 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...
0
10829
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
9640
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...
0
5854
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...
0
6058
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3282
bsmnconsultancy
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...

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.