Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
A light Focus on HASH TABLE
Posted Date: 12 Dec 2007 Resource Type: Articles Category: .NET Framework
|
Posted By: amisha Member Level: Gold Rating: Points: 10
|
Hash table is a collection ,which used key-value combinations.Usually, for searching purposes we can use hash table. You can search either by using the key or value. Here one thing we have to remember is that KEY is not only numeric,any object is acceptable here. Hash table is quiet faster compare to array.
Lets create a hash table ht as given below. To add elements to a hash table, call the Add method, passing in the key-value pairs you want to add.
NOTE: Searching through KEY is faster then searching through VALUE.
' Here we'll create a hashtable of student number and student names Dim table As Hashtable = New Hashtable()
' now, we will add elements to the hashtable, as key-value pairs table.Add(101, "anu") table.Add(201, "prasu") table.Add(301, "ram")
Now that the hash table is populated, you can search for elements in it. You are going to search both by key and value. Note that in the actual example, you will accept a value or key to find from the user, so the implementation is slightly different, but the concepts are the same.
' SEARCHING BY VALUE. Use the method ContainsValue Dim valueToFind as String = "anu"
If table.ContainsValue(valueToFind) Then Console.WriteLine("Found {0} in the list.", valueToFind ) End If
' SEARCHING BY KEY. Use the method Contains Dim keyToFind As Integer = 101
If table.Contains(keyToFind) Then Console.WriteLine("Found {0} in the list.", keyToFind) End If
You can remove entries from the hash table by using the Remove method, which you pass a key identifying the element you want to remove. You can also enumerate over the hash table using the Keys collection or the Values collection inside your instance of the hash table.
' using the Remove method to delete a particular item... table.Remove(101);
Dim o As Object
' enumerating over the collection with a foreach statement ' Note the alternate of iterating through by value is demonstrated ' For Each o In table.Values For Each o In table.Keys Console.WriteLine(o.ToString()) Next
I just explained about the basic of hash table .Try to explore more by reading different articles and pracricing by yourself. Happy Learning..
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|