当前位置:网站首页>Introduction to hashtable
Introduction to hashtable
2022-07-06 04:14:00 【xmh-sxh-1314】
Hashtable Is a non generic collection , Therefore, boxing and unpacking operations usually occur when retrieving and storing value types .
When adding an element to Hashtable when , The element will be put into the bucket according to the hash code of the key , Because it is a hash algorithm, there will be a hash function that can generate the same hash code for two different keys , The subsequent search of the key will use the hash code of the key to search only in a specific bucket , This will greatly reduce the number of key comparisons required to find an element .
Hashtable The loading factor of determines the element and Hashtable The maximum ratio of the number of elements that can be owned . The smaller the load factor , The faster the average search speed , But the memory consumption also increases . Default load factor 0.72 Usually provides the best balance between speed and size . When creating a Hashtable when , You can also specify other loading factors .
Total elements / Hashtable The number of elements you can have = Load factor
Direction Hashtable When adding elements ,Hashtable The actual loading factor of will increase . When the actual loading factor reaches the specified loading factor ,Hashtable The number of buckets in is automatically increased to greater than the current Hashtable Minimum prime number twice the number of buckets .
During capacity expansion, all data needs to be hashed again . although Hash have O(1) Data retrieval efficiency , But it usually costs a lot of space , Is to exchange space for time . therefore Hashtable Suitable for frequent reading operations , Operation types with few write operations .
Code 1 、
Copy code
static void Main(string[] args)
{
Hashtable hashtb = new Hashtable();
hashtb.Add(1, "aa");
hashtb.Add(2, "bb");
hashtb.Add(3, "cc");
hashtb.Add(4, "dd");
foreach (DictionaryEntry item in hashtb)
{
Console.WriteLine(item.Value);
item.Value = "ee";
}
Console.Read();
}
Copy code
Compilation error :item by foreach Iteration variables for , Its members cannot be modified .
reason : If you run foreach The processing statement attempts to modify the iteration variable value , Or take the variable value as ref Parameter or out Parameter passing , Then compilation errors will occur , The iteration variable is equivalent to a local read-only variable .
Code 2 、
item.Value = "ee"; Change to hashtb[item.Key] = "ee";
Operation error reporting : Set modified ; Enumeration may not be possible .
reason :.NET Framework Provide enumerators as a simple way to iterate through a collection . Enumerators only read data from the collection , Cannot be used to modify the underlying collection .
foreach Statement is used to iterate through a collection , To get the information you need , But it cannot be used to add or remove items from the source collection , Otherwise, unpredictable side effects may occur . If you need to add or remove items from the source collection , Please use for loop .
Code three 、
Copy code
Thread tr1 = new Thread(new ThreadStart(() =>
{
foreach (DictionaryEntry item in hashtb)
{
Console.WriteLine(item.Value);
}
}));
tr1.Start();
Thread tr2 = new Thread(new ThreadStart(() =>
{
for (int i = 1; i < 4; i++)
{
hashtb[i] = "ee";
Console.WriteLine(hashtb[i]);
}
}));
tr2.Start();
Copy code
Threads tr1 To read hashtable, Threads tr2 be used for foreach To enumerate modifications hashtable,
Runtime error : Set modified ; Enumeration may not be possible .
Code four 、
Copy code
// Read
Thread tr1 = new Thread(new ThreadStart(() =>
{
for (int i = 1; i <= 4; i++)
{
Console.WriteLine(hashtb[i]);
}
}));
tr1.Start();
Thread tr2 = new Thread(new ThreadStart(() =>
{
for (int i = 1; i <= 4; i++)
{
Console.WriteLine(hashtb[i]);
}
}));
tr2.Start();
// modify
Thread tr3 = new Thread(new ThreadStart(() =>
{
for (int i = 1; i <= 4; i++)
{
hashtb[i] = "ee";
}
}));
tr3.Start();
Copy code
Running results : normal
explain :Hashtable It's thread safe , Can be used by multiple reader threads and one write thread . When multithreading , If only one thread writes ( to update ) operation , It is thread safe , This allows unlocked reads ( If the writer is serialized to Hashtable)
Code five 、
Copy code
Thread tr1 = new Thread(new ThreadStart(() =>
{
lock (hashtb)
{
foreach (DictionaryEntry item in hashtb)
{
Console.WriteLine(item.Value);
}
}
}));
tr1.Start();
Thread tr2 = new Thread(new ThreadStart(() =>
{
lock (hashtb)
{
for (int i = 1; i <= 4; i++)
{
hashtb[i] = "ee";
}
}
}));
tr2.Start();
Copy code
Running results : normal
explain : Because both threads are added lock (hashtb) So it's thread safe . Enumerating a collection from beginning to end is not a thread safe process in essence . Even if a collection is synchronized , Other threads can still modify the collection , This will cause the enumerator to throw an exception . To ensure thread safety during enumeration , You can lock a collection throughout the enumeration process , Or catch exceptions caused by changes made by other threads .
Hashtable Thread safe methods provided
Hashtable Of Synchronized Static methods provide thread safe instances , as follows :
Hashtable ht = Hashtable.Synchronized(new Hashtable());
The internal implementation is as follows :
Copy code
public override void Add(object key, object value)
{
lock (this._table.SyncRoot)
{
this._table.Add(key, value);
}
}
Copy code
Output as input
because hashtable The interior is disordered , So the output is not necessarily ,hashtable The mechanism of getting data is not understood . According to the following code, you can realize first in, first out .
By controlling ArrayList Inside keys To control hashtable Output , You can also use it SortedDictionary and SortedList Implement sort collection .
Copy code
public class NoSortHashtable : Hashtable
{
private ArrayList keys = new ArrayList();
public NoSortHashtable()
{
}
public override void Add(object key, object value)
{
base.Add (key, value);
keys.Add (key);
}
public override ICollection Keys
{
get
{
return keys;
}
}
public override void Clear()
{
base.Clear ();
keys.Clear ();
}
public override void Remove(object key)
{
base.Remove (key);
keys.Remove (key);
}
边栏推荐
- BOM - location, history, pop-up box, timing
- Proof of Stirling formula
- hashlimit速率控制
- [introduction to Django] 11 web page associated MySQL single field table (add, modify, delete)
- During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
- Stable Huawei micro certification, stable Huawei cloud database service practice
- VPP性能测试
- What is the difference between gateway address and IP address in tcp/ip protocol?
- ESP32(基于Arduino)连接EMQX的Mqtt服务器上传信息与命令控制
- HotSpot VM
猜你喜欢
Solution to the problem that the root account of MySQL database cannot be logged in remotely
电脑钉钉怎么调整声音
Solution of storage bar code management system in food industry
Lora gateway Ethernet transmission
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
Stack and queue
Recommendation system (IX) PNN model (product based neural networks)
Query the number and size of records in each table in MySQL database
Yyds dry goods inventory web components series (VII) -- life cycle of custom components
Redis (replicate dictionary server) cache
随机推荐
颠覆你的认知?get和post请求的本质
Maxay paper latex template description
Overturn your cognition? The nature of get and post requests
About some basic DP -- those things about coins (the basic introduction of DP)
[disassembly] a visual air fryer. By the way, analyze the internal circuit
Record the pit of NETCORE's memory surge
Path of class file generated by idea compiling JSP page
Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share
2/13 review Backpack + monotonic queue variant
Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
Explain in simple terms node template parsing error escape is not a function
The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
SSTI template injection explanation and real problem practice
综合能力测评系统
Stack and queue
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Leetcode32 longest valid bracket (dynamic programming difficult problem)
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
绑定在游戏对象上的脚本的执行顺序
Le compte racine de la base de données MySQL ne peut pas se connecter à distance à la solution