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 哈希表中。这是因为不允许重复。
结论因此,我们可以通过将一个哈希表的数据复制或添加到另一个哈希表集合来合并两个哈希表集合。每当两个哈希表中存在公共键时,就不会添加重复的键。但程序员必须确保在添加一个哈希表的数据时进行检查,以免意外添加数据,从而导致不可预测的结果。
以上就是合并两个哈希表集合的 c# 程序的详细内容。