Home >Backend Development >C#.Net Tutorial >C# program to get keys based on values in Hashtable collection
A hash table is a collection in C# that contains items identified as key-value pairs. So unlike other data structures like stack, queue or ArrayList in C# which store a single value, a hash table in C# stores 2 values. These two values, a key-value pair, form an element of the hash table.
In a hash table, keys are unique and should not be null. Values in a hash table can be null and repeated.
In C#, the System.collections interface provides a class called "Hashtable" to represent a hash table collection. This class provides various constructors to create hash table objects, and also provides methods and properties to perform various operations on hash table objects.
In this article, we will see how to get the keys in a hash table collection based on their values.
The Hashtable class has no direct method to get keys based on values in the hash table. Therefore, we need to write a program to hash a collection of tables in order to retrieve the key given a value.
Let’s discuss how to get the key based on the value. To do this, we traverse the entire hash table based on the hash table key. We then match each value with the specified value, and if the values match, we return the corresponding key.
For traversing the hash table, we can use the following foreach loop.
foreach (string key in langCodes.Keys) { if (langCodes[key].ToString() == value) { retKey = key; } }
The iterator we use in the foreach loop to traverse the hash table is the collection of keys (langCodes.Keys). The corresponding value for each key is then compared to the specified value, and if there is a match, that specific key is returned.
The program below demonstrates this implementation.
using System; using System.Collections; class Program { public static void Main(){ // Create a Hashtable Hashtable langCodes = new Hashtable(); // Add elements to the Hashtable langCodes.Add("C++", "CPlusPlus"); langCodes.Add("C#", "CSharp"); langCodes.Add("Java", "Java"); langCodes.Add("PL", "Perl"); string value = "CSharp"; string retKey=""; foreach (string key in langCodes.Keys){ if (langCodes[key].ToString() == value){ retKey = key; } } if(retKey != ""){ Console.WriteLine("Key for the value = {0} is {1}", value,retKey); } else { Console.WriteLine("Key for the value = {0} is not present in the Hashtable", value); } } }
Here, we have a hash table called "langCodes" consisting of programming language codes and their corresponding values. Declare a string variable containing the specified value. Then use the foreach construct to iterate through the entire hash table and check for keys that are the same as the specified value. When such a key is found, the key value is returned in the variable retKey.
If there is a value in the variable retKey, we output the value as the key of the specified value. If retKey is empty, you can conclude
Key for the value = CSharp is C#
Now suppose we want to get a key with the value "JavaScript".
string value = "JavaScript";
With this change, we execute the above program and it will generate the following output.
Key for the value = JavaScript is not present in the Hashtable
Now, since there is no element in the hash table that matches the JavaScript language, the program displays the above message.
Let's take an example to simplify this topic.
The following program gets the key in a hash table given a specific value.
using System; using System.Collections; class Program { public static void Main() { // Create a Hashtable Hashtable myHashTable = new Hashtable(); // Add elements to the Hashtable myHashTable.Add("First", "Hello"); myHashTable.Add("Second", "World"); myHashTable.Add("Third", ""); myHashTable.Add("Fourth", "!"); string value = ""; string retKey=""; foreach (string key in myHashTable.Keys) { if (myHashTable[key].ToString() == value) { retKey = key; } } if(retKey != ""){ Console.WriteLine("Key for the value = {0} is {1}", value,retKey); } else { Console.WriteLine("Key for the value = {0} is not present in the Hashtable", value); } } }
In this program, we have a hash table "myHashTable" which stores the famous saying "Hello World!". But we don't store these words contiguously. Instead, we introduced a null value after the word "World".
This program is designed to get keys with null values. The generated output is shown below.
Key for the value = is Third
Here, since the hash table allows null values, when we specify a null value, the corresponding null value key will be retrieved.
Now let's change the value we want to get the key from. Next we change the value to '!' and execute the program.
string value = "!";
Key for the value = ! is Fourth
The third element in the hash table is a null value, so the last part of the quote is the fourth position in the hash table.
In this way, we can program the hash table to retrieve the key when given a specific value in the hash table. As we have seen, since hash tables allow null values, we can also pass null values and get their corresponding keys.
The above is the detailed content of C# program to get keys based on values in Hashtable collection. For more information, please follow other related articles on the PHP Chinese website!