关于Equals和GetHashCode [英] about Equals and GetHashCode

查看:82
本文介绍了关于Equals和GetHashCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


我无法弄清楚使用GetHashCode是什么意思。我知道这个

GetHashCode用于获取一个唯一的整数

值。

有人能给我一个证明这个有用的例子

GetHashCode还是完全没用?


一个意思是如果我以某种方式从当前时间得到一个整数我应该

使用它?


如果这些Equals和GetHashCode成对使用还有一个问题吗?


// Tony

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

推荐答案

否; GetHashCode()不保证唯一性 - 对于初学者来说,这是一个Int32的内容 - 这需要一个涉及Int64和i ++的简单例子来表示他们不是唯一的。


重点是,如果a.GetHashCode()和b.GetHashCode()是

*不同*那么nd b肯定不是''同样的。如果哈希码

相同,那么你需要检查a和b。这提供了一个

有效的方法来缩小我们实际需要的东西

来检查,首先排除所有不相同的东西

哈希码,

留下更小的集合。

此外,哈希表等高级结构可以使用哈希码

创建桶 - 例如,基于项目数量,它可能决定将其分成20个桶。通过使用模数

哈希码 - 即每个项目进入带有索引的桶中

GetHashCode()%bucketCount。


然后,为了找到给定的项目,它可以这样做:

*获取搜索关键字的haschode

*计算该hashcode的模数 - 我们现在知道哪个桶到[b $ b]查看[已经下载到数据的1/20]

*用正确的哈希码查看所有内容

[现在可能下降到1或2项]

*执行搜索键和存储的

项之间的实际等于,找到我们想要的那个


所以:GetHashCode()很少在没有Equals()的情况下使用[虽然

可以在没有GetHashCode()的情况下使用Equals()];这绝对是非常重要的,并且支持所有来自Dictionary<,>等的快速查找。

线性搜索(测试每个项目)会非常非常慢在一个很长的

列表中。


Marc
No; GetHashCode() does not guarantee uniqueness - for starters, it is
an Int32 - it would take a trivial example involving Int64 and i++ to
show that they aren''t unique.

The point is that if a.GetHashCode() and b.GetHashCode() are
*different* then a nd b definitely aren''t the same. If the hashcodes
are the same, then you need to check a and b. This provides an
efficient way of narrowing down the amount of things we actually need
to check, by first excluding everything that doesn''t have the same
hashcode,
leaving a much smaller set.
Further, advanced structures such as hash-tables can use the hash-code
to create "buckets" - so, for example, based on the number of items,
it might decide to split it into 20 "buckets" by taking the modulo of
the hashcode - i.e. each item goes into the bucket with index
GetHashCode() % bucketCount.

Then, to find a given item, it can do:
* get the haschode of the search key
* calculate the modulo of that hashcode - we now know which bucket to
look in [down to 1/20th of the data already]
* look in that bucket for everything with the correct hashcode
[probably down to 1 or 2 items now]
* perform the actual Equals between the search key and the stored
items, to find the one we want

So: GetHashCode() is very rarely used without Equals() [although
Equals() can be used without GetHashCode()]; it is definitely very
important, and underpins all the fast lookups from Dictionary<,>, etc.
Linear search (testing each item) would be very, very slow on a long
list.

Marc


(对于info,Dictionary<,和HashTable实际上使用更复杂的
复杂算法 - 但是这给出了理论上如何使用

哈希码的整体情况。
(for info, Dictionary<,and HashTable actually use a more
sophisticated algorithm - but that gives an overall picture of how a
hashcode might be used in theory)

如果你覆盖GetHashCode,你也应该重写Equals,以确保当两个对象相等时它们的哈希码也相同。

。至于

的使用示例,它用于Hashtable和Dictionary<,>


-

Pete

=========================================

我使用企业核心对象(域驱动设计)
http: //www.capableobjects.com/

============================== ===========
If you override GetHashCode you should also override Equals, to ensure that
when two objects are equal their hashcodes are the same as well. As for an
example of its use, it is used in Hashtable and Dictionary<,>

--
Pete
=========================================
I use Enterprise Core Objects (Domain driven design)
http://www.capableobjects.com/
=========================================


这篇关于关于Equals和GetHashCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆