合并两个哈希表集合的 C# 程序

合并两个哈希表集合的 C# 程序

C# 中的哈希表集合存储键值对。该集合中的每个元素或项目都是一个键值对,即该集合是一个双元素集合。 Key 是唯一的、非空的,用于访问哈希表中的元素。

哈希表集合是不可变的,不能有重复的元素。这意味着键值组合应该是唯一的。但是,这些值可以为空或重复。 .Net Framework 提供了一个 HashTable 类来实现哈希表集合,并包含实现哈希表所需的功能,而无需任何额外的编码。

哈希表集合中的每个元素都是一个具有两个属性的 DictionaryEntry 对象:键元素和值元素。当将元素添加到哈希表时,会自动生成哈希码。该哈希码是内部的并且是隐藏的。哈希表集合中的元素按隐藏哈希码排序。因此,哈希表元素被认为是随机选择的。

通过对哈希表集合的简要介绍,让我们看看如何合并两个哈希表集合。

如何合并两个哈希表集合?

Hashtable 类由 System 提供。集合命名空间仅包含可用于构造哈希表对象并执行添加/删除元素、计算元素数量等操作的基类库。没有提供可用于将两个哈希表合并在一起的方法/函数。

我们必须设计自己的方法来合并两个哈希表。我们知道哈希表的容量或大小是哈希表保存的元素数量。当元素插入哈希表时,哈希表的大小会通过重新分配自动增长。

因此,当我们将两个哈希表合并在一起时,我们会将一个哈希表的元素添加到另一个哈希表中。当我们添加元素时,该哈希表的大小将相应调整。

方法

创建两个哈希表对象。

使用 Add 方法用元素填充两个表。

使用键遍历第二个哈希表,如果当前项(正在遍历的键)尚不存在于第一个哈希表中,则将其每个键值对添加到第一个哈希表中。

李>

打印生成的哈希表。

注意:在添加键之前,我们会显式检查该键是否存在于哈希表中,因为哈希表不允许添加重复键。

示例

将上述方法转换为如下所示的 C# 程序。

  1. 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 哈希表中。

输出

最后,我们显示合并后的表。

  1. 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# 程序。

  1. 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 具有共同的数据。现在让我们执行这个程序来检查合并是如何发生的。

  1. 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# 程序的详细内容,更多请关注【创想鸟】其它相关文章!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

点点赞赏,手留余香

给TA打赏
共0人
还没有人赞赏,快来当第一个赞赏的人吧!
    编程技术

    C# 程序计算字符串中某个单词的出现次数

    2025-3-3 8:04:13

    编程技术

    使用 C# 查找可用磁盘空间

    2025-3-3 8:04:30

    0 条回复 A文章作者 M管理员
    欢迎您,新朋友,感谢参与互动!
      暂无讨论,说说你的看法吧
    个人中心
    购物车
    优惠劵
    今日签到
    私信列表
    搜索