字典和哈希表之间的主要区别是什么?

9 浏览
0 Comments

字典和哈希表之间的主要区别是什么?

字典和散列表之间有什么区别?我该如何决定使用哪一个?能否有人帮助我?

0
0 Comments

Dictionary和Hashtable之间的主要区别是什么?这个问题的出现的原因是因为有人想要了解这两个数据结构之间的区别。解决这个问题的方法是通过比较它们的特点和用法来说明它们之间的区别。

Dictionary是一个基本的表,有两列(键和值,都有特定的类型)和许多后添加的行。在字典中,你提供一个键,字典会给你之前通过完全相同的键添加的值。

Hashtable稍有不同。你有一个具有两列的表(键和值,都是"object"类型)。键可能不是唯一的。现在你实际上有两个表:一个具有两列:键和哈希,另一个具有两列:哈希和值。哈希是从键获取的某个整数值。结果是,尽管键可能是唯一的,但哈希可能不是。

下面是一个例子:

Hashtable ht = new Hashtable();
// 键的类型是Int32
ht[16] = "That is Int32";
// 键的类型是String
ht["Blah"] = 15;
// 键的类型是Boolean
ht[false] = "That is boolean";
// 键的类型是String
ht["Hohoho"] = false;

稍后,你可以使用键访问Hashtable中存储的任何值(如果没有这样的键,它将返回null):

Console.WriteLine("ht[{0}] = {1};", 16, ht[16] ?? "null");
Console.WriteLine("ht[{0}] = {1};", "Test", ht["Test"] ?? "null"); // 不存在
Console.WriteLine("ht[{0}] = {1};", false, ht[false] ?? "null");
Console.WriteLine("ht[{0}] = {1};", "Hohoho", ht["Hohoho"] ?? "null");

总结一下:

Dictionary是这样的:

[ Key ][ Value ]
   A      1.5
   B      1.6
   C      -8
     ....

而Hashtable可能是这样的:

[ Key ][ Hash ]
   A      1
   B      2
   C     -99
      ...
[ Hash ][ Value ]
   -99      -8
    1       1.6
    2       1.5
      ....

希望这对你有帮助。如果有人能更好地解释,请不要犹豫。谢谢,祝好运。

0
0 Comments

Hashtable is a collection class in the .NET framework that stores key-value pairs. It is an implementation of the IDictionary interface and is designed for high-performance data storage and retrieval. However, Hashtable is considered obsolete in the .NET framework, and it is recommended to use the Dictionary class instead.

The primary difference between Dictionary and Hashtable lies in their design and usage. The Hashtable class was introduced in the early versions of .NET framework and lacks some features and enhancements that are available in the Dictionary class.

One of the main reasons for the obsolescence of Hashtable is its lack of type safety. Hashtable allows storing keys and values of any type, which can lead to runtime errors if the wrong type is retrieved from the collection. On the other hand, Dictionary is a generic class that allows specifying the types of keys and values at compile-time, ensuring type safety and avoiding runtime errors.

Another reason for using Dictionary instead of Hashtable is better performance. Hashtable uses a non-generic implementation, which requires boxing and unboxing operations when storing or retrieving values. These operations can negatively impact performance, especially when working with large collections. In contrast, Dictionary is a generic class that provides better performance by eliminating the need for boxing and unboxing.

To migrate from Hashtable to Dictionary, simply replace the Hashtable declaration with Dictionary, specifying the types of keys and values. The code changes should be minimal, as both classes share similar methods and properties for adding, accessing, and removing items.

// Before migration from Hashtable to Dictionary
Hashtable hashtable = new Hashtable();
hashtable.Add("key1", "value1");
hashtable.Add("key2", "value2");
Console.WriteLine(hashtable["key1"]);
// After migration to Dictionary
Dictionary dictionary = new Dictionary();
dictionary.Add("key1", "value1");
dictionary.Add("key2", "value2");
Console.WriteLine(dictionary["key1"]);

In summary, the primary difference between Dictionary and Hashtable is that Dictionary is a generic class that provides type safety and better performance compared to the non-generic Hashtable. It is recommended to use Dictionary instead of Hashtable in modern .NET applications.

0
0 Comments

Dictionary和Hashtable之间的主要区别是它们的碰撞解决策略不同。Hashtable使用的是开放地址法,而Dictionary使用的是链表法。

开放地址法是指当插入一个新元素时,如果发生了碰撞(即两个不同的键映射到了同一个位置),则通过探测下一个空槽位来解决碰撞。这种方法的缺点是,当冲突较多时,查询性能会下降,因为需要遍历整个表来找到目标元素。

链表法是指当发生碰撞时,将冲突的元素存储在同一个位置的链表中。这样,当需要查找某个键对应的值时,只需要遍历链表即可。这种方法的优点是,在发生冲突时,不需要重新计算哈希值,只需要将元素添加到链表中即可。

对于Dictionary而言,由于它是强类型的,所以在添加或者获取元素时,不需要进行类型转换。而Hashtable则是通过Object类型进行存储,所以需要进行类型转换。

总结一下,Dictionary和Hashtable的主要区别在于碰撞解决策略和强类型特性。Dictionary使用链表法解决碰撞,并且是强类型的;而Hashtable使用开放地址法解决碰撞,并且存储的是Object类型。

更多详细信息可以参考:[http://msdn.microsoft.com/en-us/library/ms379571(v=vs.80).aspx#datastructures20_2_topic6](http://msdn.microsoft.com/en-us/library/ms379571(v=vs.80).aspx#datastructures20_2_topic6)

0