- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个关于 Dictionary 和 HashSet 在 C# 中如何工作的问题。根据我的理解,GetHashCode用于哈希表中来确定键的唯一性。
在以下 MSDN 页面上,它指出:
A hash code is a numeric value that is used to insert and identify an object in a hash-based collection such as the Dictionary class, the Hashtable class, or a type derived from the DictionaryBase class.
如果是这样,为什么当 car2 具有与 car1 相同的哈希码时,ContainsKey 和 Contains 会返回 false?如果我的理解是正确的,如果 MSDN 所说的是正确的,那么两者不应该都返回 true 吗?
class Program
{
static void Main(string[] args)
{
// Create a Dictionary and HashSet
Dictionary<Car, int> carDictionary = new Dictionary<Car, int>();
HashSet<Car> carSet = new HashSet<Car>();
// Create 3 Cars (2 generic and 1 Civic)
Car car1 = new Car();
Car car2 = new Car();
Car car3 = new Civic();
// Test hash values
int test1 = car1.GetHashCode(); // 22008501
int test2 = car2.GetHashCode(); // 22008501
int test3 = car3.GetHashCode(); // 12048305
// Add 1 generic car and 1 Civic to both Dictionary and HashSet
carDictionary.Add(car1, 1);
carDictionary.Add(car3, 1);
carSet.Add(car1);
carSet.Add(car3);
// Why are both of these false?
bool dictTest1 = carDictionary.ContainsKey(car2); // false
bool setTest1 = carSet.Contains(car2); // false
// Testing equality makes sense
bool testA = car1.Equals(car2); // false
bool testB = car1.Equals(car3); // false
}
}
class Car
{
public override int GetHashCode()
{
return 22008501;
}
}
class Civic : Car
{
public override int GetHashCode()
{
return 12048305;
}
}
最佳答案
因为ContainsKey的逻辑和这个类似。
//This is a simplified model for answering the OP's question, the real one is more complex.
private List<List<KeyValuePair<TKey,TValue>>> _buckets = //....
public bool ContainsKey(TKey key)
{
List<KeyValuePair<TKey,TValue>> bucket = _buckets[key.GetHashCode() % _buckets.Length];
foreach(var item in bucket)
{
if(key.Equals(item.Key))
return true;
}
return false;
}
GetHashCode 所做的只是获取您的 key 将放入的存储桶,它仍然必须遍历该存储桶的每个成员并通过 Equals
方法找到精确匹配。这就是为什么拥有良好的哈希码很重要的原因,桶中的项目越少,foreach
部分就会越快。最好的哈希码每个桶只有一个项目。
这是 actual code for Contains on a HashSet(Dictionary 的 ContainsKey 非常相似但更复杂)
private int[] m_buckets;
private Slot[] m_slots;
public bool Contains(T item) {
if (m_buckets != null) {
int hashCode = InternalGetHashCode(item);
// see note at "HashSet" level describing why "- 1" appears in for loop
for (int i = m_buckets[hashCode % m_buckets.Length] - 1; i >= 0; i = m_slots[i].next) {
if (m_slots[i].hashCode == hashCode && m_comparer.Equals(m_slots[i].value, item)) {
return true;
}
}
}
// either m_buckets is null or wasn't found
return false;
}
private int InternalGetHashCode(T item) {
if (item == null) {
return 0;
}
return m_comparer.GetHashCode(item) & Lower31BitMask;
}
internal struct Slot {
internal int hashCode; // Lower 31 bits of hash code, -1 if unused
internal T value;
internal int next; // Index of next entry, -1 if last
}
关于c# - 使用 Dictionary 和 HashSet 的 GetHashCode 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28593764/
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: List versus ArrayList variable type? 我正在初始化一个 HashSet就
使用初始化有什么区别 HashSet s = new HashSet(2) 和 HashSet s = new HashSet(2) ? 最佳答案 唯一的区别是第一个会向您提供有关原始类型“HashS
这个问题已经有答案了: What does it mean to "program to an interface"? (33 个回答) 已关闭 9 年前。 这两个调用有什么区别: Set insta
我知道,如果您有两个 HashSet ,您可以创建第三个将这两个 HashSet 添加。但是,出于我的目的,我需要更改之前的 456 ,查找某些条件,然后如果不满足,则再次更改设置。我的目的是,我将给
我正在尝试使用 HashSet作为其他人的 key HashSet .我找到了 this question and answer指出要实现 Hash HashSet 的特征,但我无法让我的具体案例发挥
我有两个 HashSet s,我想实现 a = a U b .如果可能的话,我想使用 HashSet::union而不是循环或其他调整。 我尝试了以下方法: use std::collections:
我有一个HashSet包含一件元素。尝试添加到集合中的新项目与现有项目相同,.equals() 。确认newElement事实上是一样的,我有一些调试打印循环通过我的 HashSet并打印每个项目:
我创建了一个 HashSet 的 HashSet。我想访问子集中的整数值。我的 HashSet 的 HashSet 是包含 Set 的所有子集的集合,如下所示:- [[], [1], [2], [1,
我必须操作存在于大 HashSet 对象下的字符串 我想知道是否有可能操纵现有的 不创建新 HashSet 对象的 HashSet 对象 以下是我当前的逻辑,其中,我想避免创建第二个 HashSet(
好奇心和效率是这个问题的原因。在某些循环运行后,我正在创建许多新的哈希集: HashSet 当前在类的顶部这样声明: private Set failedTests; 然后在代码的后面,只要我重新运行
这个问题在这里已经有了答案: How can I insert all values of one HashSet into another HashSet? (2 个回答) 9 个月前关闭。 当我尝
我有两个 Action 哈希集,如果它们出现在第二个哈希集中,我该如何删除所有相同的操作? 最佳答案 您正在寻找 ExceptWith 方法。 关于c# - 从 hashset of actions
我有代码 List> list = new ArrayList>(50); pos = 17; // just some index less than 50 list.add(pos, new Ha
我想在 HashSet 中处理一些新数据,而不需要任何旧数据或旧 HashSet 对象。旧的 HashSet 对象不在其他地方引用。 简单地做hashset = new HashSet()更好吗?让
我正在尝试学习 Rust 的诀窍,我正在玩这个小函数: fn anagrams_for(word: &str, possible_anagrams: &[&'a str]) -> HashSet {
这个问题已经有答案了: HashSet look-up complexity? (4 个回答) 已关闭 6 年前。 访问数组中特定对象/数据的平均复杂度是 O(n) ,这里 n 是数组长度。Java
我今天接受了采访,接受我采访的人对他的陈述感到困惑,询问是否有可能 TreeSet等于 HashSet但不是 HashSet等于 TreeSet .我说“不”,但据他说,答案是"is"。 怎么可能?
HashSet 没有AddRange 方法,所以我想为它写一个扩展方法。这是我的: public static void AddRange(this ICollection collection, I
我编写了一个接受 HashSet 的函数范围。我想通过 HashSet其中 SomeEnumeration具有基础类型 byte .有没有简单的方法可以做到这一点? public enum SomeE
我想存储一些不允许重复的像素位置,所以首先想到的是 HashSet或类似的类(class)。然而,与 HashSet 之类的东西相比,这似乎非常慢. 例如,这段代码: HashSet points =
我是一名优秀的程序员,十分优秀!