The difference between C# Hashtable and Dictionary

The difference between C# Hashtable and Dictionary

The difference between Hashtable and Dictionary
1. The elements added by Hashtable are of object type, while the elements added by Dictionary are of specified (T) type.
2.Dictionary has the advantages of generics, faster reading speed, and more fully utilized capacity.
3. In the foreach loop, Dictionary can get sub-objects, but Hashtable cannot.

Hash table Hashtable

Using Hashtable needs to introduce the namespace System.Collections. The key/value key-value pairs in Hashtable are all object types, so Hashtable can support any type of key/value key-value pairs. Each object in Hashtable is a key-value pair stored in a DictionaryEntry object.

common attributes

Attributes describe
Count Get the number of key-value pairs contained in the Hashtable
Keys Get the collection of all keys in Hashtable
Values Get the collection of all values ​​​​in the Hashtable

common method

method describe
public virtual void Add(object key, object value); Adds an element with the specified key and value to the Hashtable
public virtual bool ContainsKey(object key); Determine whether the Hashtable contains the specified key
public virtual bool ContainsValue(object value); Determine whether the Hashtable contains the specified value
public virtual void Remove(object key); Removes the element with the specified key from the Hashtable
public virtual void Clear(); Remove all elements from Hashtable

Note: Since the IDictionary interface is the base interface for a non-generic collection of key/value pairs, and each element is a key/value pair and is stored in a DictionaryEntry object, the element type is neither the type of the key nor the type of the value type, but DictionaryEntry type. (Leave a hole, I didn't figure out what it was)

Hashtable ht = new Hashtable();

ht.Add(1, true);
ht.Add(2, "FALSE");

Console.WriteLine("当前包含键值对个数:{0}", ht.Count);//输出:2

Console.WriteLine("---- 通过键求键值对 ----");
foreach (var key in ht.Keys)
{
    
    
    Console.WriteLine("{0} - {1}", key, ht[key]);//输出:2-"FALSE" 1-True 
}

Console.WriteLine("---- 添加后值的集合 ----");
foreach (var value in ht.Values)
{
    
    
    Console.WriteLine("{0}", value);//输出:FALSE True
}

if (ht.ContainsKey(1))
{
    
    
    Console.WriteLine("Hashtable中包含键 1 .");//输出
}

if(ht.ContainsValue(true))
{
    
    
    Console.WriteLine("Hashtable中包含值 true .");//输出
}

ht.Remove(1);
Console.WriteLine("---- 移除键 1 之后 ----");
foreach(DictionaryEntry de in ht)
{
    
    
    Console.WriteLine("{0} - {1}", de.Key, de.Value);//输出:2 - FALSE
}


ht.Clear();
Console.WriteLine("---- 移除全部之后 ----");
foreach (DictionaryEntry de in ht)
{
    
    
    Console.WriteLine("{0} ------- {1}", de.Key, de.Value);//无输出
}

Console.ReadKey();

DictionaryDictionary

Dictionary<TKey, TValue> is contained in the System.Collections.Generic namespace. Dictionary<TKey, TValue> can support any type of key/value key-value pairs specified . So the Dictionary<TKey, TValue> object can only add key/value pairs of the specified type. Each object in Dictionary<TKey, TValue> is a key-value pair stored in a KeyValuePair<TKey, TValue> object.

common attributes

Attributes describe
Count Get the number of key-value pairs contained in Dictionary<TKey, TValue>
Item Get a key-value pair in Dictionary<TKey, TValue>
Keys Get a collection of all keys in Dictionary<TKey, TValue>
Values Get a collection of all values ​​in Dictionary<TKey, TValue>

common method

method describe
public void Add(TKey key, TValue value); Add the specified key and value to the dictionary
public bool ContainsKey(TKey key); Determine whether Dictionary<TKey, TValue> contains the specified key
public bool ContainsValue(TValue value); Determine whether Dictionary<TKey, TValue> contains the specified value
public bool Remove(TKey key); Removes the element of the specified key from Dictionary<TKey, TValue>
public void Clear(); Remove all key-value pairs from Dictionary<TKey, TValue>
Dictionary<int, string> dc = new Dictionary<int, string>();

dc.Add(21002121, "张三");
dc.Add(21002122, "李四");

Console.WriteLine("字典中的键值对个数:{0}", dc.Count);//输出:2

Console.WriteLine("---- 添加之后键值对 ----");
foreach (var item in dc)
{
    
    
    Console.WriteLine("{0} - {1}", item.Key, item.Value);//输出:21002121 - 张三   21002122 - 李四
}

Console.WriteLine("---- 通过键求键值对 ----");
foreach (var key in dc.Keys)
{
    
    
    Console.WriteLine("{0} - {1}", key, dc[key]);//输出:21002121 - 张三   21002122 - 李四
}

Console.WriteLine("---- 添加后值的集合 ----");
foreach (var value in dc.Values)
{
    
    
    Console.WriteLine("{0}", value);//张三 李四
}

if(dc.ContainsKey(21002121))
{
    
    
    Console.WriteLine("Dictionary中包含键 21002121 .");//输出
}

if(dc.ContainsValue("李四"))
{
    
    
    Console.WriteLine("Dictionary中包含值 李四 .");//输出
}

dc.Remove(21002121);

Console.WriteLine("---- 移除键 21002121 之后 ----");
foreach (KeyValuePair<int, string> kp in dc)
{
    
    
    Console.WriteLine("{0} - {1}", kp.Key, kp.Value);//输出:21002122 - 李四
}

dc.Clear();
Console.WriteLine("---- 移除全部之后 ----");
foreach (KeyValuePair<int, string> kp in dc)
{
    
    
    Console.WriteLine("{0} ----- {1}", kp.Key, kp.Value);//无输出
}


Console.ReadKey();

Hashtable vs. Dictionary Insertion Time Comparison

Dictionary<int, int> dc = new Dictionary<int, int>();
Hashtable ht = new Hashtable();

int count = 10000000;//插入的次数 1千万
Stopwatch sw = new Stopwatch();
sw.Start();//计时
for(int i = 0; i < count; i++)
{
    
    
    dc.Add(i, i);
}
sw.Stop();
Console.WriteLine("字典Dictionary<TKey, TValue>耗时(毫秒):" + sw.ElapsedMilliseconds);//输出:388

sw.Restart();
for (int i = 0; i < count; i++)
{
    
    
    ht.Add(i, i);
}
sw.Stop();
Console.WriteLine("哈希表Hashtable耗时(毫秒):" + sw.ElapsedMilliseconds);//输出:2497
Console.ReadLine();

Note: The time consumed is not unique. For computers with average performance, it is recommended to reduce the number of insertions

Due to the limited energy of the author, some mistakes and omissions will inevitably appear in the article. Experts and netizens are welcome to criticize and correct me.

Guess you like

Origin blog.csdn.net/qq_46051312/article/details/123632469