最近很多时候都在用C#的集合类型,发现个问题:什么时候用Dictionary和Hashtable?哪个一个性能上指的是查询的时候更高效呢,于是MSDN上搜了一下,找到了如下文档,大概意思是Hashtable利用一个缓存池来存储被唯一赋予按哈西算法生成的hashcode的值对象,类似与数据库中的索引,查询的时候读取每个对象的hashcode从缓存池中进行查询,最关键的是最后一段,Dictionary也是利用hash算法存储对象的,不过存储非简单类型对象的时候也就是自定的类型和Object类型的对象性能上要比hasttable类型高。
下面引自MSDN:
Hashtable and Dictionary Collection Types
The Hashtable class and the Dictionary<(Of <(TKey, TValue>)>) generic class implement the IDictionary interface. The Dictionary<(Of <(TKey, TValue>)>) generic class also implements the IDictionary<(Of <(TKey, TValue>)>) generic interface. Therefore, each element in these collections is a key-and-value pair.
A Hashtable object consists of buckets that contain the elements of the collection. 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, generated using a hash function and based on the key of the element.
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.
Each object that is used as an element in a Hashtable must be able to generate a hash code for itself using an implementation of the GetHashCode method. 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.
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. 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.
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. 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. In contrast, “stressed” and “desserts” would have the same hash code and would be in the same bucket.
The Dictionary<(Of <(TKey, TValue>)>) class has the same functionality as the Hashtable class. A Dictionary<(Of <(TKey, TValue>)>) of a specific type (other than Object) has better performance than a Hashtable for value types because the elements of Hashtable are of type Object and, therefore, boxing and unboxing typically occur if storing or retrieving a value type.
[文档]Dictionary与Hashtable
相关推荐
- 员工考勤打卡时,如何避免非本人代替打卡? - 华为云开发者联盟 - 博客园
- Web Components从技术解析到生态应用个人心得指北 - zhoulujun - 博客园
- 【经典问题】mysql和redis数据一致性问题 - Scotyzh - 博客园
- vs出现错误,无法启动 Visual Studio。StreamJsonRpc.ConnectionLostException:在请求完成之前,与远程的JSON-RPC连接已丢失_客服专区-CSDN问答
- 【转】Chrome内核浏览器打开网页报 错误代码: ERR_TIMED_OUT - m_lm的个人空间 - OSCHINA - 中文开源技术交流社区
- ASP.NET Core WebApi配置跨域_asp.net core webapi 跨域-CSDN博客
- C# 怎么用OpenCVSharp4实现图片表格识别
- ChatGPT 本地部署及搭建_孟郎郎的博客-CSDN博客