相关疑难解决方法(0)

重写System.Object.GetHashCode的最佳算法是什么?

在.NET GetHashCode方法中,很多地方都使用.NET 方法.特别是在快速查找集合中的项目或确定相等性时.是否有关于如何GetHashCode为我的自定义类实现覆盖的标准算法/最佳实践,因此我不会降低性能?

.net algorithm hashcode gethashcode

1389
推荐指数
14
解决办法
19万
查看次数

与此哈希码函数进行HashCode冲突的可能性有多大?

在以下场景中,与下面的函数发生HashCode冲突的可能性有多大.

  1. 使用键[0],键[1],键[2],键[3]的随机int值
  2. 使用具有以下约束的随机键值
    • 键[0] <1,000,000
    • key [1] <10,000
    • 关键[2] <1,000
    • 关键[3] <1,000

假设我们有1000万个对象.

int[] key=new int[4];    
public override int GetHashCode()
{
    // Use large prime multiples to create a unique hash key
    // Create the hash offsets using a "even powers of 2 minus 1" method, which gives 
    // primes most of the time.  
    int hashKey = 0;
    hashKey += 2047 * key[0];
    hashKey += 8191 * key[1];
    hashKey += 32767 * key[2];
    hashKey += 131071 * key[3];
    return hashKey; …
Run Code Online (Sandbox Code Playgroud)

c# hashcode data-structures

5
推荐指数
2
解决办法
1574
查看次数

标签 统计

hashcode ×2

.net ×1

algorithm ×1

c# ×1

data-structures ×1

gethashcode ×1