哈希表是基于键的哈希码排列的键/值对的集合。换句话说, 哈希表用于创建使用哈希表进行存储的集合。它通常通过计算每个键的哈希码来优化查找, 并自动将其存储到另一个存储篮中, 并且当你当时从哈希表访问值时, 它会将哈希码与指定的键进行匹配。这是非通用类型的集合, 在系统集合命名空间。
重要事项:
- 在Hashtable中, 键不能为null, 但值可以为null。
- 在哈希表中, 键对象必须是不可变的, 只要它们在哈希表中用作键即可。
- 哈希表的容量是哈希表可以容纳的元素数。
- 哈希表中的每个键对象都提供了哈希函数。
- Hashtable类实现了词典, ICollection, IEnumerable, 可序列化, IDeserializationCallback和可克隆接口。
- 在哈希表中, 你可以存储相同类型和不同类型的元素。
- 作为键/值对的哈希表的元素存储在DictionaryEntry中, 因此你也可以将键/值对强制转换为字典条目.
- 在哈希表中, 密钥必须唯一。不允许重复的密钥。
如何创建哈希表?
哈希表类提供16用于创建哈希表的不同类型的构造函数, 这里我们仅使用Hashtable()构造函数。要了解有关Hashtable的构造函数的更多信息, 请参阅C#|哈希表类该构造函数用于创建Hashtable类的实例, 该实例为空并具有默认的初始容量, 负载因子, 哈希码提供程序和比较器。现在, 让我们看看如何使用Hashtable()构造函数创建哈希表:
第1步:借助以下命令在程序中包含System.Collections命名空间使用关键词:
using System.Collections;
第2步:使用Hashtable类创建一个哈希表, 如下所示:
Hashtable hashtable_name = new Hashtable();
第三步:如果要在哈希表中添加键/值对, 请使用Add()方法在哈希表中添加元素。你还可以在不使用哈希表的情况下将键/值对存储在哈希表中Add()方法.
例子:
// C# program to illustrate how
// to create a hashtable
using System;
using System.Collections;
class GFG {
// Main Method
static public void Main()
{
// Create a hashtable
// Using Hashtable class
Hashtable my_hashtable1 = new Hashtable();
// Adding key/value pair
// in the hashtable
// Using Add() method
my_hashtable1.Add( "A1" , "Welcome" );
my_hashtable1.Add( "A2" , "to" );
my_hashtable1.Add( "A3" , "lsbin" );
Console.WriteLine( "Key and Value pairs from my_hashtable1:" );
foreach (DictionaryEntry ele1 in my_hashtable1)
{
Console.WriteLine( "{0} and {1} " , ele1.Key, ele1.Value);
}
// Create another hashtable
// Using Hashtable class
// and adding key/value pairs
// without using Add method
Hashtable my_hashtable2 = new Hashtable() {
{1, "hello" }, {2, 234}, {3, 230.45}, {4, null }};
Console.WriteLine( "Key and Value pairs from my_hashtable2:" );
foreach ( var ele2 in my_hashtable2.Keys)
{
Console.WriteLine( "{0}and {1}" , ele2, my_hashtable2[ele2]);
}
}
}
输出如下:
Key and Value pairs from my_hashtable1:
A3 and lsbin
A2 and to
A1 and Welcome
Key and Value pairs from my_hashtable2:
4and
3and 230.45
2and 234
1and hello
如何从哈希表中删除元素?
在哈希表中, 允许你从哈希表中删除元素。 Hashtable类提供了两种不同的方法来删除元素, 这些方法是:
- 明确:此方法用于从哈希表中删除所有对象。
- 去掉:此方法用于从哈希表中删除具有指定键的元素。
例子:
// C# program to illustrate how
// remove elements from the hashtable
using System;
using System.Collections;
class GFG {
// Main Method
static public void Main()
{
// Create a hashtable
// Using Hashtable class
Hashtable my_hashtable = new Hashtable();
// Adding key/value pair
// in the hashtable
// Using Add() method
my_hashtable.Add( "A1" , "Welcome" );
my_hashtable.Add( "A2" , "to" );
my_hashtable.Add( "A3" , "lsbin" );
// Using remove method
// remove A2 key/value pair
my_hashtable.Remove( "A2" );
Console.WriteLine( "Key and Value pairs :" );
foreach (DictionaryEntry ele1 in my_hashtable)
{
Console.WriteLine( "{0} and {1} " , ele1.Key, ele1.Value);
}
// Before using Clear method
Console.WriteLine( "Total number of elements present" +
" in my_hashtable:{0}" , my_hashtable.Count);
my_hashtable.Clear();
// After using Clear method
Console.WriteLine( "Total number of elements present in" +
" my_hashtable:{0}" , my_hashtable.Count);
}
}
输出如下:
Key and Value pairs :
A3 and lsbin
A1 and Welcome
Total number of elements present in my_hashtable:2
Total number of elements present in my_hashtable:0
如何检查哈希表中键/值对的可用性?
在哈希表中, 可以使用以下方法检查给定对是否存在:
- 包含:此方法用于检查哈希表是否包含特定键。
- ContainsKey:此方法还用于检查哈希表是否包含特定键。
- 包含值:此方法用于检查Hashtable是否包含特定值。
例子:
// C# program to illustrate how
// to check key/value present
// in the hashtable or not
using System;
using System.Collections;
class GFG {
// Main Method
static public void Main()
{
// Create a hashtable
// Using Hashtable class
Hashtable my_hashtable = new Hashtable();
// Adding key/value pair in the hashtable
// Using Add() method
my_hashtable.Add( "A1" , "Welcome" );
my_hashtable.Add( "A2" , "to" );
my_hashtable.Add( "A3" , "lsbin" );
// Determine whether the given
// key present or not
// using Contains method
Console.WriteLine(my_hashtable.Contains( "A3" ));
Console.WriteLine(my_hashtable.Contains(12));
Console.WriteLine();
// Determine whether the given
// key present or not
// using ContainsKey method
Console.WriteLine(my_hashtable.ContainsKey( "A1" ));
Console.WriteLine(my_hashtable.ContainsKey(1));
Console.WriteLine();
// Determine whether the given
// value present or not
// using ContainsValue method
Console.WriteLine(my_hashtable.ContainsValue( "geeks" ));
Console.WriteLine(my_hashtable.ContainsValue( "to" ));
}
}
输出如下:
True
False
True
False
False
True
更多C#开发相关内容请参考:lsbin - IT开发技术:https://www.lsbin.com/
查看一下更多C#数据结构相关内容: