哈希表和字典集合类型Hashtable and Dictionary Collection Types

System.Collections.Hashtable 类以及 System.Collections.Generic.Dictionary<TKey,TValue>System.Collections.Concurrent.ConcurrentDictionary<TKey,TValue> 泛型类实现 System.Collections.IDictionary 接口。The System.Collections.Hashtable class, and the System.Collections.Generic.Dictionary<TKey,TValue> and System.Collections.Concurrent.ConcurrentDictionary<TKey,TValue> generic classes, implement the System.Collections.IDictionary interface. Dictionary<TKey,TValue> 泛型类还实现 IDictionary<TKey,TValue> 泛型接口。The Dictionary<TKey,TValue> generic class also implements the IDictionary<TKey,TValue> generic interface. 因此,这些集合中的每个元素都是一个键值对。Therefore, each element in these collections is a key-and-value pair.

Hashtable 由包含集合元素的存储桶组成。A Hashtable object consists of buckets that contain the elements of the collection. 存储桶是 Hashtable 中元素的虚拟子组,与在大多数集合中进行搜索和检索相比,其搜索和检索更加容易和快速。A bucket is a virtual subgroup of elements within the Hashtable, which makes searching and retrieving easier and faster than in most collections. 每个存储桶都与一个哈希代码相关联,该哈希代码通过哈希函数生成并基于元素的键。Each bucket is associated with a hash code, which is generated using a hash function and is based on the key of the element.

泛型 HashSet<T> 类是用于包含唯一元素的无序集合。The generic HashSet<T> class is an unordered collection for containing unique elements.

哈希函数是一种算法,返回基于键的数值哈希代码。A hash function is an algorithm that returns a numeric hash code based on a key. 该键是所存储对象的某个属性的值。The key is the value of some property of the object being stored. 哈希函数必须始终返回同一个键的同一哈希代码。A hash function must always return the same hash code for the same key. 哈希函数有可能为两个不同的键生成相同的哈希代码,但从哈希表中检索元素时,为每个唯一的键生成唯一哈希代码的哈希函数具有更好的性能。It is possible for a hash function to generate the same hash code for two different keys, but a hash function that generates a unique hash code for each unique key results in better performance when retrieving elements from the hash table.

Hashtable 中用作元素的每个对象必须能够通过使用 GetHashCode 方法的实现为自身生成哈希代码。Each object that is used as an element in a Hashtable must be able to generate a hash code for itself by using an implementation of the GetHashCode method. 但是,还可以为 Hashtable 中的所有元素指定哈希函数,方法是使用接受 IHashCodeProvider 实现作为其参数之一的 Hashtable 构造函数。However, you can also specify a hash function for all elements in a Hashtable by using a Hashtable constructor that accepts an IHashCodeProvider implementation as one of its parameters.

当将对象添加到 Hashtable时,其存储在与哈希代码相关联的存储桶中,此哈希代码匹配该对象的哈希代码。When an object is added to a Hashtable, it is stored in the bucket that is associated with the hash code that matches the object's hash code. 当在 Hashtable 中对一个值进行搜索时,则为该值生成哈希代码,并搜索与该哈希代码相关联的存储桶。When a value is being searched for in the Hashtable, the hash code is generated for that value, and the bucket associated with that hash code is searched.

例如,用于字符串的哈希函数可能采用字符串中每个字符的 ASCII 代码,并将它们加总以生成哈希代码。For example, a hash function for a string might take the ASCII codes of each character in the string and add them together to generate a hash code. 字符串“picnic”的哈希代码可能与字符串“basket”的哈希代码不同;因此,字符串“picnic”和“basket”可能在不同的存储桶中。The string "picnic" would have a hash code that is different from the hash code for the string "basket"; therefore, the strings "picnic" and "basket" would be in different buckets. 与此相反,“stressed”和“desserts”可能具有相同的哈希代码,并且位于同一个存储桶中。In contrast, "stressed" and "desserts" would have the same hash code and would be in the same bucket.

Dictionary<TKey,TValue>ConcurrentDictionary<TKey,TValue> 类具有与 Hashtable 类相同的功能。The Dictionary<TKey,TValue> and ConcurrentDictionary<TKey,TValue> classes have the same functionality as the Hashtable class. 特定类型(不包括 Object)的 Dictionary<TKey,TValue>Hashtable 相比可为值类型提供更好的性能。A Dictionary<TKey,TValue> of a specific type (other than Object) provides better performance than a Hashtable for value types. 这是因为 Hashtable 的元素属于 Object 类型;因此,装箱和取消装箱通常发生在存储或检索值类型时。This is because the elements of Hashtable are of type Object; therefore, boxing and unboxing typically occur when you store or retrieve a value type. 可能有多个线程同时访问该集合时,应使用 ConcurrentDictionary<TKey,TValue> 类。The ConcurrentDictionary<TKey,TValue> class should be used when multiple threads might be accessing the collection simultaneously.

请参阅See also