- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在阅读 Troelsen 的《C# 和 .NET 4.5 框架》一书。书中有一节他有一个重写的例子
public virtual int GetHashCode(); // Defined in System.Object
他说(以下引自 Troelsen 的书):
Given that the String class already has a solid hash code algorithm that is using the character data of the String to compute a hash value, if you can identify a piece of field data on your class that should be unique for all instances (such as a Social Security number), simply call GetHashCode() on that point of field data.
基本上他说的就是某个类有一个成员(自动只读属性)
public string SSN {get; }
并且该类的每个实例都将具有唯一的字符串值。现在,假设
// s1 and s2 are strings
s1.GetHashCode() != s2.GetHashCode(); // Assumption: If this true then s1 == s2 is true
他的推理是有效的。但是,当我阅读 String.GetHashCode() 时:
If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code.
我想你明白我要用这个做什么。我想是我遗漏了什么,如果是这样,请指出正确的方向。
谢谢!
最佳答案
GetHashCode
的目的|不是为对象生成唯一标识符,而是实现基于hash tables的数据结构,例如 Dictionary<K, V>
或 HashSet<T>
.
需要哈希函数来确保如果 x == y
, 然后 x.GetHashCode() == y.GetHashCode()
,但反之则不为真:两个不同的对象可以具有相同的哈希码。这种情况称为哈希冲突。
哈希表结构在发生碰撞时仍有效,但它们运行速度较慢,因为您的程序必须花费时间来消除哪个您正在搜索的碰撞对象的歧义.因此,好的 散列函数会尽量减少冲突。 (请注意,如果一个类有超过 232 个可能值,则完全避免冲突在数学上是不可能的,因为 pigeonhole principle。)
那你怎么写好GetHashCode
你类(class)的实现?做一些复杂的数学运算,将类(class)的每个字段转换为 int
, 然后对其进行分析以确定其中系数的最佳值?
根据 Troelsen 的说法,不。就拿你的“最独特”string
现场并调用GetHashCode()
在那上面。编写 System.String.GetHashCode
的开发人员知道他们在做什么,所以只要使用它,您就会自动利用他们的“可靠的哈希码算法”。
关于C# - String.GetHashCode() -> 不要用作唯一标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33746270/
不要 重复自己* 如何为现代机器学习设计开源库 🤗 Transformers 设计理念 “不要重复自己 (Don’t Repeat Yourself)” ,或 DR
我有这种情况。我有 2 个分支,master 和 develop。 在开发分支上我有一些文件,比如说 tools.js .如果我需要更改这个文件,提交它并将其推送到 Github 开发分支。 一切完成
我要上传图片但首先我想查看图像预览,然后当用户单击另一个 asp:button 时,保存图像。 对于预览部分,我使用以下代码: jQuery(document).ready(functi
我是一名编程初学者,现在从 Python 切换到 Clojure。我正在研究一个质数代码,但我没有弄错。我也想练习递归 (defn true-division [n i] (= (/ n i) (
运行 Python 2.7 执行时: $ python client.py get_emails -a "åäö" 我得到: usage: client.py get_emails [-h] [-a
根据我对“告诉-不要-询问”原则的理解,我的其他类不应该能够调用存储在任何其他类中的数据。因此,根据这一原则, setter/getter 是不受欢迎的。为了防止访问数据,它们通常写为: class
我在寻找什么: 我想使用SIMPLE模式最小化的出色功能,同时仅禁用一项特定功能(禁用内联本地功能)。 更新:答案是否定的,根据我的设置是不可能的。 但对于我来说,鉴于我正在使用Grails,有一种解
根据我对“告诉-不要-询问”原则的理解,我的其他类不应该能够调用存储在任何其他类中的数据。因此,根据这一原则, setter/getter 是不受欢迎的。为了防止访问数据,它们通常写为: class
是否可以不 float 具有样式 UITableViewStylePlain 的 UITableView 的节标题? 我正在 build AcaniChat, an open-source versi
当二进制文件、swfs、jar 和 flvs 在本地更改时,我尝试 pull 入更改,git 尝试 merge 它们并报告冲突。 然后,我分支到一个临时分支,提交本地更改的二进制文件,并在 pull
我正在尝试使用 Pex 来测试一些代码。我有一个具有四个具体实现的抽象类。我为四种具体类型中的每一种都创建了工厂方法。我还为抽象类型创建了一个,除了 this nice thread。说明,Pex 不
我正在将 asp.net mvc 3 和 razor 用于一个项目。在某些情况下,我需要从 Controller 序列化一个数组,将其放入 View 数据并将其分配给一个 js 对象。但是当我使用 输
是否可以让一个 webpack 开发服务器配置多个入口点(网站上有多个页面),每个入口点都有不同的配置? 具体来说,我希望将一个条目(页面的 JS 代码)分成 block ,但不要将另一个条目(带有已
我需要使用 hibernate 将 InputStream 或 byte[] (个人资料图像)保存在表中。这里的代码: @Override public void actualizarFotoPerf
我在一个 android 项目中同时拥有 GMS 和 HMS。 GMS 版本有效,但 HMS 不调用 onMapReady 回调。这是代码: private var mMap: HuaweiM
我有一个单元测试文件: module X04PatMatTest where import AssertError import Test.HUnit import X04PatMat ... 和 h
是否可以将 c++ 库包装到 c 中? 我该怎么做? 有现成的工具吗? (需要访问现有的 c++ 库,但只能使用 C) 最佳答案 您可以用 C 编写面向对象的代码,因此如果它是面向对象的 C++ 库,
我有一个 JSP 页面,它接受 SQL 查询,执行它们然后将结果返回到一个表中。一些结果偶尔会在其中包含 HTML 标记,即 - 结果将返回: This is the returned result!
我有一个问题。 我需要帮助。我一直在寻找解决方案大约 5 个小时。不幸的是没有成功。 我的问题是我有几个 Storyboard并且没有使用 Segue 创建。 我希望将选定的 Tableviewcel
当我尝试运行以下代码时: #include void main() { char *a[10] = {"hi", "hello", "how"}; int i = 0, j = 0;
我是一名优秀的程序员,十分优秀!