如果不能保证字符串或整数的getHashCode()唯一,为什么要使用它? [英] If getHashCode() for string or integer is not guaranteed to be unique why use it?

查看:135
本文介绍了如果不能保证字符串或整数的getHashCode()唯一,为什么要使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在标题中写道.

如果在您的应用程序中使用getHashCode()不安全,为什么要使用它呢? (用于字符串和整数) 我想用它来与方法相交,但Linq模型中的方法除外 或创建自己的IEqualityCompare类. 感觉像是一次机会-如果不是100%安全?

If its not safe to use getHashCode() in your application, why use it? (for string and integer) I want to use it to intersect methods and except metods in Linq models or create my own IEqualityCompare class. It feels like a chance - if its not 100% secure?

还是我错过了什么?

https://docs.microsoft.com/

重要

如果两个字符串对象相等,则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.

不能保证哈希码本身是稳定的.对于单个版本的.NET,跨.NET实现,跨.NET版本以及跨.NET平台(例如32位和64位)的相同字符串的哈希码可能会有所不同.在某些情况下,它们甚至可能因应用程序域而异.这意味着同一程序的两次后续运行可能返回不同的哈希码.

The hash code itself is not guaranteed to be stable. Hash codes for identical strings can differ across .NET implementations, across .NET versions, and across .NET platforms (such as 32-bit and 64-bit) for a single version of .NET. In some cases, they can even differ by application domain. This implies that two subsequent runs of the same program may return different hash codes.

因此,永远不要在哈希代码所在的应用程序域之外使用哈希代码 创建后,永远不要将它们用作集合中的关键字段,并且永远不要保留它们.

As a result, hash codes should never be used outside of the application domain in which they were created, they should never be used as key fields in a collection, and they should never be persisted.

最后,如果出现以下情况,请不要使用哈希码,而不要使用密码哈希函数返回的值: 您需要加密强度高的哈希.对于加密散列,请使用从 System.Security.Cryptography.HashAlgorithm或System.Security.Cryptography.KeyedHashAlgorithm类.

Finally, don't use the hash code instead of a value returned by a cryptographic hashing function if you need a cryptographically strong hash. For cryptographic hashes, use a class derived from the System.Security.Cryptography.HashAlgorithm or System.Security.Cryptography.KeyedHashAlgorithm class.

有关哈希码的更多信息,请参见Object.GetHashCode.

For more information about hash codes, see Object.GetHashCode.

推荐答案

我认为让您感到困惑的是,您认为哈希码映射到一个值的地址,但并非完全一样.

I think what makes you confused is that you think that, that hash code maps to an address of a value, but it's not exactly like that.

想象它像书架,并且哈希码映射到书架的地址.如果其中两个具有相同的HashCode,则将它们放在相同的书架中,并且在其中有3本书的书架的地址中,字典仅检查书架上的3本书,而不是所有书.因此,越独特的哈希码,字典查找就越快.

Imagine it like bookshelves, and Hash Code maps to address of a shelf. If two of them have the same HashCode will be placed in the same Shelf, and having the address of a shelf with 3 books in it, dictionary only checks the three books on the shelf and not all the books. So the more unique hash codes are, the faster the dictionary lookup is.

创建IEqualityComparer时,如果可以使GetHashCode()返回唯一值,则使用它的Dictionary或HashSet的执行速度将比重复项多时要快.

When you create IEqualityComparer if you can make the GetHashCode() to return unique values, the Dictionary or HashSet using it will perform faster than when there are many duplicates.

选中此示例:

public int GetShashCode(string ojb)
{
     return obj.Length;
}

尽管它比循环遍历整个字符串要快得多,但是它不是很独特(尽管它是有效的)

although it makes it much faster than looping through the whole strings, but it is not very unique (although it is valid)

此示例也有效,但选择更糟:

This example is also valid but even a worse choice:

public int GetShashCode(string ojb)
{
     return (int)obj[0];
}

根据您可以猜测的字符串的内容,您可以编写更好的哈希码(例如,您知道它是以下格式的社会保险号:"XXX-XX-XXXX",每个X代表一个数字)将是一个不错的选择:

Based on the content of the string that you can guess, you can make much better hashcodes (for example you know that that it is a social security number in this format: "XXX-XX-XXXX" which each X represent a digit) will be a great choice:

public int GetShashCode(string ojb)
{
     return int.Parse(obj.Replace("-",""));
}

这篇关于如果不能保证字符串或整数的getHashCode()唯一,为什么要使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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