Dictionary (Dictionary) and hash table (Hashtable)

The use and difference of Dictionary and Hashtable

dictionary:

1. A dictionary is a typical key-value pair data structure, and each element is composed of a key-value pair (key key and value value).
2. This data structure can access elements through a certain key, so dictionaries are also called mappings or hash tables.
3. The main feature of the dictionary is to quickly find the value according to the key, and you can also add and delete elements freely. This is a bit like a List, but unlike List, List is stored continuously and directly addressed. A dictionary is like a linked list, which is stored non-contiguously, so deleting an element does not require moving subsequent elements, and there is no performance overhead of moving subsequent elements in memory.
4. The speed of retrieving values ​​through keys is very fast, close to O(1). This is because the Dictionary class is implemented as a hash table. Dictionary has no order, which is different from list lists. There are sequence.
Keys must be unique, while values ​​need not be unique.
5. The keys and values ​​in the dictionary are all object types, so they can be of any type (such as: string, int, custom type, etc.)

//创建字典
Dictionary<string,int> dict = new Dictionary<string,int>();
//添加元素
dict.Add();
//删除元素
dict.Remove();
//删除字典所有元素
dict.Clear();
//循环遍历字典
foreach(var item in dict)
{
    
    
    Debug.Log(item.Key+item.Value);
//if判断字典是否存在要找的内存
   if(item.Key=="")
   {
    
    
        Debug.Log("存在");
   }
}

//ContainsKey判断是否存在Key,返回一个Bool类型
if(dict.ContainsKey(""))
{
    
    
    Debug.Log("存在");
}

//ContainsValue判断是否存在Value,返回一个Bool类型
if(dict.ContainsValue()))
{
    
    
    Debug.Log("存在");
}

//TryGetValue判断Key值是否在字典中,返回一个Bool值给a(0或1)
int a;
dict.TryGetValue("",a);
Debug.Log(a);

Hash table:

1. Hash table The Hashtable class implements the IDictionary interface, and the values ​​in the collection are also accessed in the form of key-value pairs (key/value).
2. A hash table, also known as a hash table, in which each element stores a value in the form of a key-value pair (key/value).
3. It should be noted that the key is case-sensitive.
4. The keys and values ​​in the Hashtable are object types, so the Hashtable supports any type of key-value pair.

using System.Collections.Generic;//使用哈希表时,必须要引用这个命名空间
//创建哈希表(使用方式和字典基本相同)
Hashtable hstable = new Hashtable();
//添加数据
hstable.Add(1,"2");
hstable.Add("a",true);
//删除数据
hstable.Remove("a");
bool t - hstable.ContainsKey("a");//判断是否存在key值(返回bool类型)
bool i = hstable.ContainsValue(true);//判断是否存在Value值(返回bool类型)
//遍历哈希表
foreach(DictionaryEntry item in hstable)
{
    
    
    Debug.Log(item.key+" "+ item.Value);
}

The difference between a dictionary and a hash table:

1. The difference between multi-threading.
Single-threaded dictionaries have the advantages of generics, fast reading speed, and more fully utilized capacity;
Hash forms are written by threads and read by multiple threads.
2. Thread-safe
dictionaries are not thread-safe, and locks must be artificially increased The statement is protected and affects efficiency.
The hash table can call the Synchronized() method to obtain a fully thread-safe type.
3. Data insertion order
The sorting of the dictionary is based on the sorting of insertion (note: the order will be disrupted after deleting the node), so the dictionary is more effective when the order needs to be reflected.
Hash tables do not insert data in order.
4. Indexing efficiency
The dictionary is more efficient when indexing data in a single thread, and the reading speed is fast, but the efficiency will decrease when the amount of data is large. The
indexing method of the hash table is processed by hashing, and it is processed when the amount of data is large. It is more efficient, so the hash table is more suitable for use in various occasions that need to improve efficiency, such as object caching, tree recursive algorithm replacement, etc.

Guess you like

Origin blog.csdn.net/qq_45598937/article/details/127284883