Two Hashtable classes
1. Namespace
System.Collections
2. definition
It is used to process and represent key/value-like key pairs, where key is usually used for fast lookup, while key is case-sensitive; value is used to store values corresponding to key. The key/value key pairs in Hashtable are all object types, so Hashtable can support any type of key/value key pairs.
3. Use of hash tables
(1) Some data will be queried at high frequencies
(2) Large amount of data
(3) Query fields contain string types
(4) Data types are not unique
4. Common attributes
Count: Gets the number of key/value pairs contained in Hashtable.
Keys: Get an ICollection interface that contains all the keys.
Values: Get an ICollection interface with all the key values
IsFixedSize: Gets a value indicating whether Hashtable has a fixed size.
IsReadOnly: Gets a value indicating whether Hashtable is read-only.
Item[Object]: Gets or sets the value associated with the specified key.
5. Common methods
Add(Object, Object): Add elements with specified keys and values to Hashtable.
Clear(): Remove all elements from Hashtable.
Contains(Object): Determines whether Hashtable contains a specific key.
ContainsKey(Object): Determines whether Hashtable contains a specific key.
ContainsValue(Object): Determines whether Hashtable contains a specific value.
CopyTo(Array, Int32): Copies elements in Hashtable from a specified location into a one-dimensional array.
Remove(Object): Remove elements with specified keys from Hashtable.
The sample code is as follows
Hashtable myH = new Hashtable();
myH.Add("Dolores", 18);
myH["Alice"] = "Queen";
6. Traversing Hash Table
To traverse the hash table, you need to use Dictionary Entry Object. The code is as follows:
foreach (DictionaryEntry de in myH)
{
Console.WriteLine(de.Key);
Console.WriteLine(de.Value);
}
foreach (object i in myH.Keys)
{
Console.WriteLine(i);
}
foreach (object i in myH.Values )
{
Console.WriteLine(i);
}
7. Examples of hash tables: computer Glossary
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace CH10_Hashtable_Demo
{
public partial class Form1 : Form
{
Hashtable glossary = new Hashtable();
public Form1()
{
InitializeComponent();
}
private void BuildGlossary(Hashtable g)
{
string[] words ;
string line;
StreamReader inFile = File.OpenText(@"D:\VS\C#APP Data Structure and AlgorithmsCH10_Hashtable DemoWords.txt“);
while (inFile.Peek ()!= -1)
{
line = inFile.ReadLine();
words = line.Split(',');
g.Add(words[0], words[1]);
}
inFile.Close();
}
private void DisplayeWord(Hashtable g)
{
object[] words = new Object[g.Count];
g.Keys.CopyTo(words, 0);
for (int i = 0; i < words.Length; i++)
{
if(words [i] != null )
listBox1.Items.Add(words[i]);
}
}
private void Form1_Load(object sender, EventArgs e)
{
BuildGlossary(glossary);
DisplayeWord(glossary);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
object word;
word = listBox1.SelectedItem;
textBox1.Text = glossary[word].ToString ();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}