C# 中的哈希表集合存储键值对。该集合中的每个元素或项目都是一个键值对,即该集合是一个双元素集合。 Key 是唯一的、非空的,用于访问哈希表中的元素。
哈希表集合是不可变的,不能有重复的元素。这意味着键值组合应该是唯一的。但是,这些值可以为空或重复。 .Net Framework 提供了一个 HashTable 类来实现哈希表集合,并包含实现哈希表所需的功能,而无需任何额外的编码。
哈希表集合中的每个元素都是一个具有两个属性的 DictionaryEntry 对象:键元素和值元素。当将元素添加到哈希表时,会自动生成哈希码。该哈希码是内部的并且是隐藏的。哈希表集合中的元素按隐藏哈希码排序。因此,哈希表元素被认为是随机选择的。
通过对哈希表集合的简要介绍,让我们看看如何合并两个哈希表集合。
如何合并两个哈希表集合?
Hashtable 类由 System 提供。集合命名空间仅包含可用于构造哈希表对象并执行添加/删除元素、计算元素数量等操作的基类库。没有提供可用于将两个哈希表合并在一起的方法/函数。
我们必须设计自己的方法来合并两个哈希表。我们知道哈希表的容量或大小是哈希表保存的元素数量。当元素插入哈希表时,哈希表的大小会通过重新分配自动增长。
因此,当我们将两个哈希表合并在一起时,我们会将一个哈希表的元素添加到另一个哈希表中。当我们添加元素时,该哈希表的大小将相应调整。
方法
创建两个哈希表对象。
使用 Add 方法用元素填充两个表。
使用键遍历第二个哈希表,如果当前项(正在遍历的键)尚不存在于第一个哈希表中,则将其每个键值对添加到第一个哈希表中。
李>打印生成的哈希表。
注意:在添加键之前,我们会显式检查该键是否存在于哈希表中,因为哈希表不允许添加重复键。
示例
将上述方法转换为如下所示的 C# 程序。
'using System;
using System. Collections;
class MyHashTable {
static public void Main() {
Hashtable indianNumberSystem = new Hashtable();
indianNumberSystem.Add(1,"Ones");
indianNumberSystem.Add(10,"Tens");
indianNumberSystem.Add(100,"Hundred");
indianNumberSystem.Add(1000,"Thousand");
Console.WriteLine("Contents of indianNumberSystem hashtable:");
foreach(DictionaryEntry ele1 in indianNumberSystem){
Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value);
}
Hashtable langCodes = new Hashtable();
langCodes.Add("C++","CPlusPlus");
langCodes.Add("C#","CSharp");
langCodes.Add("Java","Java");
langCodes.Add("PL","Perl");
Console.WriteLine("Contents of langCodes Hashtable:");
foreach(DictionaryEntry ele1 in indianNumberSystem){
Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value);
}
foreach (DictionaryEntry entry in langCodes) {
if(!indianNumberSystem.ContainsKey(entry.Key)) {
indianNumberSystem.Add(entry.Key, entry.Value);
}}
Console.WriteLine("Key, Value pairs after merging langCodes to indianNumberSystem:");
foreach(DictionaryEntry ele1 in indianNumberSystem){
Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value);
}
}
}
这里我们有两个哈希表,即 indianNumberSystem 和 langCodes。
哈希表 indianNumberSystem 具有以下数据,
1 |
“一个” |
10 |
“十” |
100 |
“一百” |
1000 |
“千” |
哈希表 langCodes 具有以下数据。
C++ |
“CPlusPlus” |
C# |
“CSharp” |
Java |
“Java” |
PL |
“Perl” |
我们首先显示这两个表的内容。然后我们使用 langCodes 哈希表的键来遍历它。在遍历循环中,我们首先检查哈希表 indianNumberSystem 是否具有相同的键。如果该键不存在,我们将当前键指向的 langCodes 元素添加到 indianNumberSystem 哈希表中。
输出
最后,我们显示合并后的表。
'Contents of indianNumberSystem hashtable:
1000 (Thousand)
10 (Tens)
100 (Hundred)
1 (Ones)
Contents of langCodes Hashtable:
1000 (Thousand)
10 (Tens)
100 (Hundred)
1 (Ones)
Key, Value pairs after merging langCodes to indianNumberSystem:
100 (Hundred)
1000 (Thousand)
PL (Perl)
10 (Tens)
C# (CSharp)
Java (Java)
C++ (CPlusPlus)
1 (Ones)
从生成的输出中我们可以看到两个表都正确合并。
示例
现在让我们考虑另一个示例,即下面给出的 C# 程序。
'using System;
using System. Collections;
using System.Collections.Generic;
class MyHashTable {
static public void Main() {
Hashtable indianNumberSystem = new Hashtable();
indianNumberSystem.Add(1,"Ones");
indianNumberSystem.Add(10,"Tens");
indianNumberSystem.Add(100,"Hundred");
indianNumberSystem.Add(1000,"Thousand");
Console.WriteLine("Contents of indianNumberSystem hashtable:");
foreach(DictionaryEntry ele1 in indianNumberSystem){
Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value);
}
Hashtable NumberNames = new Hashtable();
NumberNames.Add(1,"One");
NumberNames.Add(2,"Two");
NumberNames.Add(3,"Three");
NumberNames.Add(4,"Four");
Console.WriteLine("Contents of NumberNames Hashtable:");
foreach(DictionaryEntry ele1 in NumberNames){
Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value);
}
foreach (DictionaryEntry entry in NumberNames) {
if(!indianNumberSystem.ContainsKey(entry.Key)) {
indianNumberSystem.Add(entry.Key, entry.Value);
}}
Console.WriteLine("Key, Value pairs after merging NumberNames to indianNumberSystem:");
foreach(DictionaryEntry ele1 in indianNumberSystem){
Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value);
}
}
}
该程序与前一个程序相同,只是我们用 NumberNames 哈希表替换了 langCodes 哈希表。 NumberNames 哈希表具有以下元素。
1 |
“一” |
2 |
“两个” |
3 |
“三 |
4 |
“四” |
输出
正如我们所见,哈希表 indianNumberSystem 和 NumberNames 具有共同的数据。现在让我们执行这个程序来检查合并是如何发生的。
'Contents of indianNumberSystem hashtable:
1000 (Thousand)
10 (Tens)
100 (Hundred)
1 (Ones)
Contents of NumberNames Hashtable:
4 (Four)
3 (Three)
2 (Two)
1 (One)
Key, Value pairs after merging NumberNames to indianNumberSystem:
100 (Hundred)
1000 (Thousand)
10 (Tens)
4 (Four)
3 (Three)
2 (Two)
1 (Ones)
从上面的输出中可以看出,NumberNames 中的数据元素 (key=1) 没有添加到 indianNumberSystem 哈希表中。这是因为不允许重复。
结论
因此,我们可以通过将一个哈希表的数据复制或添加到另一个哈希表集合来合并两个哈希表集合。每当两个哈希表中存在公共键时,就不会添加重复的键。但程序员必须确保在添加一个哈希表的数据时进行检查,以免意外添加数据,从而导致不可预测的结果。