如何使用指定键从 C# 中的 HashTable 集合中获取值

2023年 8月 29日 36.5k 0

如何使用指定键从 C# 中的 HashTable 集合中获取值

A hashtable is a collection of key−value pairs. We can access key−value pairs using an iterator. We can also access the keys of the hashtable in a collection. Similarly, we can access the values in a hashtable. Given a hashtable, it is also possible to access the value of a specified key or matching key of a specified value.

让我们讨论一下如何在给定键的哈希表集合中访问一个值。

如何使用指定的键从Hashtable集合中获取值?

Here, we have to obtain a value from the key−value pair of hashtables when a key is given.

考虑以下哈希表。

{“US", "New York"}
{"FR", "Paris"}
{"UK", "London"}
{"IN", "Mumbai"}
{"GER", "Berlin"}

登录后复制

Here, let’s suppose we have to find the value for the key “UK”. So we have to traverse the hashtable to find out if the hashtable contains the key = UK. Once the key=” UK” is found, we can access its corresponding value as hashtable[key].

Example

The program that exactly performs the above operation is shown below −

using System;
using System.Collections;
class MyHashTable {
// Main Method
static public void Main() {

// Create a hashtable instance
Hashtable Citytable = new Hashtable();

// Adding key/value pair in the hashtable using Add() method
Citytable.Add("US", "New York");
Citytable.Add("FR", "Paris");
Citytable.Add("UK", "London");
Citytable.Add("IN", "Mumbai");
Citytable.Add("GER", "Berlin");

String key;
Console.WriteLine("Enter the key whose value is to be printed:");
key = Console.ReadLine();
if(key != ""){
if(Citytable.Contains(key) == true){
string keyval = (string)Citytable[key];
Console.WriteLine("The value of key {0} = {1}", key,keyval);
}
else
Console.WriteLine ("Value for the key= {0} does not exist", key);
}
Console.ReadKey();
}
}

登录后复制

在上面的程序中,我们定义了一个哈希表。然后用户输入要获取值的键。一旦键被读取为输入,我们首先确定键是否为null或空。这是因为哈希表的键不应为null。因此,如果用户输入一个空值,我们将不会继续查找值。

因此,如果键不为空,我们将检查哈希表是否包含指定的键。为此,我们使用C#中的哈希表集合方法 Contains() ,如果键存在于哈希表中则返回true,如果键不存在则返回false。

如果 Contains() 方法返回 true,则我们只需访问该特定键的值。

string keyval = (string)Citytable[key];

登录后复制

Then this value is displayed to the user.

Output

Enter the key whose value is to be printed:
FR
The value of key FR = Paris

登录后复制

在这个输出中,用户执行了程序并输入了键值为FR。由于这个键已经存在于哈希表中,该键对应的值成功返回。

现在,如果我们输入一个在哈希表中不存在的键值?

Let’s execute the program again. Now we do not have a key in our hashtable for the country Canada. Let’s enter the key as CAN for Canada. The output is shown below.

Output

Enter the key whose value is to be printed:
CAN
Value for the key= CAN do not exist

登录后复制

在这里,由于哈希表中不包含 key=CAN,程序返回值不存在的消息。

以这种方式,我们可以开发一个交互式程序,从哈希表集合中找到指定键的值。

Let’s take another example to find the value given a key using a hashtable.

Here we will consider the following hashtable containing numbers and their corresponding number names.

{“1.1", "One point One"}
{"1.2", "One point Two"}
{"1.3", "One point Three"}
{"1.4", "One point Four"}
{"1.5", "One point Five"}

登录后复制

类似于前面的示例,这里我们也会要求用户输入要查找值的键,然后在哈希表中搜索指定的键并显示其值。

Example 2

Below given is the program to do that same.

using System;
using System.Collections;
class MyHashTable {
// Main Method
static public void Main() {

// Create a hashtable instance
Hashtable Numbernames = new Hashtable();

// Adding key/value pair in the hashtable using Add() method
Numbernames.Add("1.1", "One point One");
Numbernames.Add("1.2", "One point Two");
Numbernames.Add("1.3", "One point Three");
Numbernames.Add("1.4", "One point Four");
Numbernames.Add("1.5", "One point Five");

String key = "1.4";
if(key != ""){
if(Numbernames.Contains(key) == true){
string keyval = (string)Numbernames[key];
if(keyval != "")
Console.WriteLine("The value of key {0} = {1}", key,keyval);
else
Console.WriteLine("The value for key = {0} does not exist", key);
}
else
Console.WriteLine ("The key= {0} does not exist in the NumberNames hashtable", key);
}
Console.ReadKey();
}
}

登录后复制

The program is the same as the previous example except for the hashtable and an extra condition we have specified to check for an empty value. This is because it can so happen that a specified key might be present in the hashtable, but its corresponding value might be empty. Secondly, we are not reading user input in this program, instead, we have directly used a key = “1.4” and we print out the value of this key. So we introduced one more check in this program. Hence this program now checks −

  • If the key specified is empty

  • If the key is not empty, the program checks if the hashtable contains the key.

  • If the hashtable contains the key, then it retrieves the value for the key. If the value is not empty, then the program displays the value.

  • If the value is empty, the appropriate message is displayed.

Output

The value of key 1.4 = One point Four

登录后复制

This output is generated when we specify a correct key that is present in the hashtable.

在本文中,我们看到了如何通过键从哈希表集合中获取值。我们还通过几个编程示例展示了不同的输出,以清晰地说明概念。在我们接下来的文章中,我们将继续讨论哈希表的相关主题。

以上就是如何使用指定键从 C# 中的 HashTable 集合中获取值的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论