The Hashtable Constructor
We can create an empty Hashtable by using a constructor. A constructor method starts with the "New" keyword, which initializes an object of a class. In this case, the Hashtable itself is the class and we need to initialize it first.
Hashtable htBooks = new Hashtable();
Dim htBooks As New Hashtable
Once initialized, we can now start adding values to the Hashtable, each value attached with a unique key.
Note: You cannot add duplicate keys to the Hashtable. In addition, a key cannot be null. However, it will accept duplicate or null values in the Hashtable.
Method Add()
Use the add() method to add elements with key and value to the Hashtable. As a small demo, we will add a <div> element to our project to display the added elements.
using System; using System.Collections; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Hashtable htBooks = new Hashtable(); htBooks.Add(1, "Computer Architecture"); htBooks.Add(2, "Advanced Composite Materials"); htBooks.Add(3, "Asp.Net 4 Blue Book"); divBooks.InnerHtml = "Showing a list of Books <br />"; foreach (DictionaryEntry DE in htBooks) { divBooks.InnerHtml = divBooks.InnerText + "<br /> " + DE.Key + " - " + DE.Value; } } }
Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load Dim htBooks As New Hashtable htBooks.Add(1, "Computer Architecture") htBooks.Add(2, "Advanced Composite Materials") htBooks.Add(3, "Asp.Net 4 Blue Book") divBooks.InnerHtml = "Showing a list of Books <br />" For Each DE As DictionaryEntry In htBooks divBooks.InnerHtml = divBooks.InnerText & "<br /> " & DE.Key & " - " & DE.Value Next End Sub End Class
Output
3 – Asp.Net 4 Blue Book
2 – Advanced Composite Materials
1 – Computer Architecture
It displays the elements using LIFO (Last In First Out) method.
To retrieve the values from the Hashtable, we will use the DictionaryEntry type. We will initialize the type and create an object, which actually stores the pair (key, value). Finally loop through the object using foreach statement (For Each in Vb.Net) to retrieve the key and value pair, and display the list of books in the DIV element.
Method Remove()
Use the Remove() method to remove specified keys from the Hashtable list. It takes a single parameter “Key as Object”. You will need to specify the key inside the brackets. In our example, we will remove the Book with the key BUSI (Business) from the Hashtable and add a new book called the “The Final Frontier” in the “SPA” (Space) category.
using System; using System.Collections; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Hashtable htBooks = new Hashtable(); htBooks.Add("CMP", "Computer Architecture"); htBooks.Add("MSc", "Advanced Composite Materials"); htBooks.Add("BUSI", "Challenging Times"); // SHOW ORIGINAL LIST BEFORE APPLYING Remove(). foreach (DictionaryEntry DE in htBooks) { divBooks.InnerHtml = divBooks.InnerText + "<br /> " + DE.Key + " - " + DE.Value; } htBooks.Remove("BUSI"); divBooks.InnerHtml = divBooks.InnerText + "<br /> <br /> " + " Book <b>Challenging Times</b> Removed from Hashtable <br /> "; htBooks.Add("SPA", "The Final Frontier"); foreach (DictionaryEntry DE in htBooks) { divBooks.InnerHtml = divBooks.InnerText + "<br /> " + DE.Key + " - " + DE.Value; } divBooks.InnerHtml = divBooks.InnerText + "<br /> <br /> " + " Book <b>The Final Frontier</b> Added to Hashtable "; } }
Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load Dim htBooks As New Hashtable htBooks.Add("CMP", "Computer Architecture") htBooks.Add("MSc", "Advanced Composite Materials") htBooks.Add("BUSI", "Challenging Times") ' SHOW ORIGINAL LIST BEFORE APPLYING Remove(). For Each DE As DictionaryEntry In htBooks divBooks.InnerHtml = divBooks.InnerText & "<br /> " & DE.Key & " - " & DE.Value Next htBooks.Remove("BUSI") divBooks.InnerHtml = divBooks.InnerText & "<br /> <br /> " & " Book <b>Challenging Times</b> Removed from Hashtable <br /> " htBooks.Add("SPA", "The Final Frontier") For Each DE As DictionaryEntry In htBooks divBooks.InnerHtml = divBooks.InnerText & "<br /> " & DE.Key & " - " & DE.Value Next divBooks.InnerHtml = divBooks.InnerText & "<br /> <br /> " & " Book <b>The Final Frontier</b> Added to Hashtable " End Sub End Class
Property "Count"
Normally, a Hashtable will have a big list of elements. To get the exact count of elements, we can use the Count property.
for (int i = 0; i <= htBooks.Count; i++)
{
divBooks.InnerHtml = divBooks.InnerText + "<br /> " + htBooks[i] ;
}
For i = 0 To htBooks.Count
divBooks.InnerHtml = divBooks.InnerText & "<br /> " & htBooks(i)
Next
Method clear()
The clear() method will remove all the elements from the Hashtable. However, we can re initialize the Hashtable to make way for a fresh list of elements.
protected void Page_Load(object sender, EventArgs e) { Hashtable htBooks = new Hashtable(); htBooks.Add(1, "Computer Architecture"); htBooks.Add(2, "Advanced Composite Materials"); htBooks.Add(3, "Asp.Net 4 Blue Book"); divBooks.InnerHtml = "Hashtable elements before clear(): " + htBooks.Count; htBooks.Clear(); // CLEAR THE HASHTABLE. divBooks.InnerHtml = divBooks.InnerHtml + "<br /> Hashtable elements after clear(): " + htBooks.Count; }
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load Dim htBooks As New Hashtable htBooks.Add(1, "Computer Architecture") htBooks.Add(2, "Advanced Composite Materials") htBooks.Add(3, "Asp.Net 4 Blue Book") divBooks.InnerHtml = "Hashtable elements before clear(): " & htBooks.Count htBooks.Clear() ' CLEAR THE HASHTABLE. divBooks.InnerHtml = divBooks.InnerHtml & "<br /> Hashtable elements after clear(): " & htBooks.Count End Sub
Method Contains()
The Contains() method returns a boolean (true or false) value if the specified key is found in the Hashtable.
The example below uses the Contains() method to check if the keys CMP and PHY (Computer and Physics) exists in the Hashtable.
Hashtable htBooks = new Hashtable(); htBooks.Add("CMP", "Computer Architecture"); htBooks.Add("MSc", "Advanced Composite Materials"); htBooks.Add("BUS", "Challenging Times"); divBooks.InnerHtml = htBooks.Contains("CMP").ToString(); divBooks.InnerHtml = divBooks.InnerHtml + "<br /> " + htBooks.Contains("PHY").ToString();
Dim htBooks As New Hashtable htBooks.Add("CMP", "Computer Architecture") htBooks.Add("MSc", "Advanced Composite Materials") htBooks.Add("BUS", "Challenging Times") divBooks.InnerHtml = htBooks.Contains("CMP") divBooks.InnerHtml = divBooks.InnerHtml & "<br /> " & htBooks.Contains("PHY")
Output
True
False
Optionally, you can use a similar function in this category, also called the “ContainsKey()”. This method does exactly what the “Contains()” method did, in the above example.
htBooks.ContainsKey("PHY").toString();
Method ContainsValue()
The ContainsValue() method returns a boolean (true or false) value if the specified value is found in the Hashtable.
divBooks.InnerHtml = htBooks.ContainsValue("Challenging Times").ToString();
divBooks.InnerHtml = htBooks.ContainsValue("Challenging Times")
Property "Keys"
We can display all the listed keys in a Hashtable using the property keys. The keys can either be an integer or string value. Depending upon the keys type, we can use the foreach loop (For Each in Vb.Net) to extract all the keys and display it.
Hashtable htBooks = new Hashtable();
htBooks.Add("CMP", "Computer Architecture");
htBooks.Add("MSc", "Advanced Composite Materials");
htBooks.Add("BUSI", "Challenging Times");
foreach (string key in htBooks.Keys)
{
divBooks.InnerHtml = divBooks.InnerHtml + "<br /> " + key;
}
Dim htBooks As New Hashtable
htBooks.Add("CMP", "Computer Architecture")
htBooks.Add("MSc", "Advanced Composite Materials")
htBooks.Add("BUSI", "Challenging Times")
For Each key As String In htBooks.Keys
divBooks.InnerHtml = divBooks.InnerHtml & "<br /> " & key
Next
Output
CMP
Msc
BUSI
Property "Values"
We have checked the list of keys in the above example; similarly, we can extract all the values from the Hashtable using the Values property.
Hashtable htBooks = new Hashtable();
htBooks.Add("CMP", "Computer Architecture");
htBooks.Add("MSc", "Advanced Composite Materials");
htBooks.Add("BUS", "Challenging Times");
foreach (string val in htBooks.Values)
{
divBooks.InnerHtml = divBooks.InnerHtml + "<br /> " + val;
}
Dim htBooks As New Hashtable
htBooks.Add("CMP", "Computer Architecture")
htBooks.Add("MSc", "Advanced Composite Materials")
htBooks.Add("BUSI", "Challenging Times")
For Each val As String In htBooks.Values
divBooks.InnerHtml = divBooks.InnerHtml & "<br /> " & val
Next
Output
Computer Architecture
Advanced Composite Materials
Challenging Times