C#程序检查哈希表中是否存在值


哈希表是有组织的键值对集合,其中键根据使用哈希函数计算的键的哈希码进行排列。虽然键在哈希表中应该是唯一的且非空,但值可以为空并且可以重复。

哈希表中的元素使用键进行访问。在 C# 中,“Hashtable” 类表示哈希表集合。此类提供了各种属性和方法,我们可以使用它们来执行操作和访问哈希表中的数据。

在本文中,我们将了解如何确定特定值是否出现在哈希表中。

如何在哈希表中检查值是否存在?

要检查值是否在哈希表中存在,我们可以使用 Hashtable 类提供的“containsValue”方法。此方法返回一个布尔值,指示指定值是否出现在哈希表中。

在继续编程示例之前,让我们先看一下此方法。

ContainsValue 方法

语法 − public virtual bool ContainsValue (object value);

描述 − 用于查找 Hashtable 是否包含指定值。

参数 − 要在哈希表中查找的值 (object)。可以是空值。

返回值 − 布尔值:true => 哈希表包含一个具有指定值的元素。

False => 哈希表不包含具有指定值的元素。

命名空间 − System.Collections

现在让我们看几个编程示例,在这些示例中,我们检查指定值是否出现在哈希表中。

示例

下面给出了第一个程序,用于检查值是否出现在哈希表中。

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");
      
      // use ContainsValue method to check if the HashTable contains the 
      //required Value or not.
      if (langCodes.ContainsValue("CSharp"))
         Console.WriteLine("langCodes hashtable contain the Value = CSharp");
      else
         Console.WriteLine("langCodes hashtable doesn't contain the Value = CSharp");
   }
}

上面的程序声明了一个 langCodes 哈希表,其中包含语言代码和语言名称分别作为键和值。接下来,我们有一个“if”结构,它检查值“CSharp”是否出现在哈希表中。如果存在,它将相应地显示消息。

输出

程序的输出如下所示。

langCodes hashtable contain the Value = CSharp

由于值 = CSharp 出现在哈希表中,因此程序给出了以上消息。

现在将 ContainsValue 方法的参数更改为“C#”,即键而不是值。

if (langCodes.ContainsValue("C#"))

现在执行带有此更改的上述程序。

输出

在这种情况下,由于值“C#”不出现在哈希表中,因此程序将返回相应的消息。所以我们会得到 -

langCodes hashtable doesn't contain the Value = CSharp

示例

现在让我们看下面的例子。

using System;
using System.Collections;
class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable NumberNames = new Hashtable();
      // Add elements to the Hashtable
      NumberNames.Add(1, "One");
      NumberNames.Add(3, "Three");
      NumberNames.Add(5, "Five");
      NumberNames.Add(7, "Seven");
      // use ContainsValue method to check if the HashTable contains the
      //required Value or not.
      if (NumberNames.ContainsValue("Two"))
         Console.WriteLine("NumberNames hashtable contain the Value = Two");
      else
         Console.WriteLine("NumberNames hashtable doesn't contain the Value = Two");
      if (NumberNames.ContainsValue("Five"))
         Console.WriteLine("NumberNames hashtable contain the Value = Five");
      else
         Console.WriteLine("NumberNames hashtable doesn't contain the Value = Five");
   }
}

此程序有一个“NumberNames”表,其中数字作为键,其对应的名称作为值。在这里,我们首先使用“containsKey()”方法检查哈希表是否包含值 = Two。接下来,我们使用 containsKey() 方法检查值 = “Five”。

输出

程序的输出如下所示。

NumberNames hashtable doesn't contain the Value = Two
NumberNames hashtable contain the Value = Five

从程序中定义的哈希表可以看出,它不包含值 = Two,但包含值 = Five。因此,程序相应地给出了消息。

结论

因此,使用 C# 中 Hashtable 类的“containsKey()”方法,我们可以确定是否在哈希表中存在具有特定值的元素。根据值是否存在,我们可以输出相应的结果,或者在复杂程序的情况下,继续执行相应的代码。

因此,当我们必须检查指定值是否出现在哈希表中,然后采取相应的行动时,“containsKey()”方法变得很有用。

更新于:2022-12-22

1K+ 浏览量

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.