Easily learn the hash table of C

  • 2021-08-21 21:12:21
  • OfStack

In the C # language, there is also an array of key/value combinations organized for quick search. This array is called associative array, also known as hash table (Hashtable).
Hash tables are also used under the System. Collection namespace to process and represent key-value pairs like key/value, where key is typically used for quick lookups, key is case sensitive, and key must be 1-only. It does not have a valid sort, it does an intrinsic sort, and value is used to store the value corresponding to key. The key/value key-value pairs in the hash table are all of object type, so the hash table can support any type of key/value key-value pairs. Each element of the hash table is a key-value pair stored in an DictionaryEntry object (the so-called DictionaryEntry structure defines a dictionary key-value pair that can be set or retrieved, with one key attribute and one value attribute, representing keys and values respectively).
The biggest advantage of hash table is that the time consumed by data storage and search is greatly reduced, which can be regarded as constant time almost, but the cost is only to consume more memory. However, with more and more memory available, it is worthwhile to trade space for time. In addition, easy coding is also one of its characteristics.
1. Adding Hashtable elements
Hashtable provides an key/value key-value pair Add method that adds elements. This method has two parameters, one is a key, which functions as an index in an array to help find, and the other is a value, which can be regarded as an element in an array. Its format is: Hashtable Object. Add (Key, Value)
Example 1. Adding elements of Hashtable object by the above method


<span style="font-size:18px;">using System; 
using System.Collections;// Namespace to be added  
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace  Hash table  
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   Hashtable al = new Hashtable(); 
   Console.WriteLine(" Before adding al The number of elements of is: "+al.Count); 
   al.Add("1", "a"); 
   al.Add("2", "b"); 
   al.Add("3", "c"); 
   Console.WriteLine(" After adding al The number of elements of is: "+al.Count); 
   Console.ReadLine(); 
  } 
 } 
}</span> 

The output result is: The number of elements of al before adding is: 0
The number of elements of al after addition is: 3
2. Hashtable Element Deletion
Elements of the Hashtable object can be deleted by the Remove method and the Clear method.
(1). The Clear method clears all elements in the format: Hashtable Object. Clear ()
(2). The Remove method accepts an key parameter to remove an key/value key-value pair in the format: Hashtable Object. Remove ()
Example 2. Deleting Hashtable elements by the above method


<span style="font-size:18px;">using System; 
using System.Collections;// Namespace to be added  
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace  Hash table  
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   Hashtable al = new Hashtable(); 
   Console.WriteLine(" Before adding al The number of elements of is: "+al.Count); 
   al.Add("1", "a"); 
   al.Add("2", "b"); 
   al.Add("3", "c"); 
   Console.WriteLine(" After adding al The number of elements of is: "+al.Count); 
   al.Remove("3"); 
   Console.WriteLine(" Delete 3 Posterior al The number of elements of is: "+al.Count); 
   Console.ReadLine(); 
  } 
 } 
}</span> 

The output result is: The number of elements of al before adding is: 0
The number of elements of al after addition is: 3
The number of elements of al after C is deleted is: 2
3. Traversal of Hashtable elements
Traversing the hash table requires DictionaryEntry (dictionary key/value pair) Object.
Example 3. Using foreach statement to traverse hash table


<span style="font-size:18px;">using System; 
using System.Collections;// Namespace to be added  
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace  Hash table  
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   Hashtable al = new Hashtable(); 
   Console.WriteLine(" Before adding al The number of elements of is: "+al.Count); 
   al.Add("1", "a"); 
   al.Add("2", "b"); 
   al.Add("3", "c"); 
   Console.WriteLine(" After adding al The number of elements of is: "+al.Count); 
   foreach (DictionaryEntry t in al) 
   { 
    Console.Write(" Key position: "+t.Key+"  Values are: "); 
    Console.WriteLine(t.Value); 
   } 
   Console.ReadLine(); 
  } 
 } 
}</span> 

The output result is: The number of elements of al before adding is: 0
The number of elements of al after addition is: 3
Key position: 1 Value: a
Key position: 2 Value: b
Key position: 3 Value: c
4. Lookup of Hashtable Elements
The Hashtable collection provides three lookup methods for finding elements in Hashtable, the Contains method, the ContainsKe and method, and the ContainsValue method.
Contains method, ContainsKey method is based on the key value of Hashtable to find, if found, return the index from 0 of the last matching item, otherwise return-1, its format is:
Hashtable Object. Contains (key Value) or Hashtable Object. ContainsKey (key Value)
The ContainValue method is based on the value value of Hashtable. If it is found, it returns the index of the last matching item starting from 0, otherwise, it returns-1, and its format is: Hashtable Object. ContainsValue (Value Value)
Example 4. Using the above method to find Hashtable elements


<span style="font-size:18px;">using System; 
using System.Collections;// Namespace to be added  
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace  Hash table  
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   Hashtable al = new Hashtable(); 
   Console.WriteLine(" Before adding al The number of elements of is: "+al.Count); 
   al.Add("1", "a"); 
   al.Add("2", "b"); 
   al.Add("3", "c"); 
   Console.WriteLine(" After adding al The number of elements of is: "+al.Count); 
   if (al.Contains("1")) 
   { 
    Console.WriteLine("1 Existence al Medium "); 
   } 
   if (al.ContainsKey("2")) 
   { 
    Console.WriteLine("2 Existence al Medium "); 
   } 
   if (al.ContainsValue("c")) 
   { 
    Console.WriteLine("c Existence al Medium "); 
   } 
   Console.ReadLine(); 
  } 
 } 
}</span> 

The output result is: The number of elements of al before adding is: 0
The number of elements of al after addition is: 3
1 exists in al
2 exists in al
c exists in al

The above is about C # hash table related introduction, hoping to help everyone's study


Related articles: