C# Program to Replace Items in One Hashtable with Another Hashtable


The hashtable collection in C# is a non-generic collection of key-value pairs that are organized based on the key’s hash code. The key is used to access the elements in the hashtable collection. Hashing helps us to efficiently retrieve data and eliminates the need for costly techniques of searching data. Hashing technique used the key itself to locate the data. This hashtable key is immutable and does not allow duplicate entries in the hashtable.

The Hashtable class is defined in the System.Collections namespace provides the base class libraries for hashtable collection in C#. This Hashtable class is used to create a collection of key-value pairs that use a hashtable for storage. The lookup for a particular key is optimized by calculating the hash code of the key and storing it in another basket internally. When we access the value from the hashtable, it matches the hashcode with the specified key.

In this tutorial, we are going to discuss a way to replace items or elements in one hashtable with that of another hashtable.

How to Replace Items in One Hashtable with Another Hashtable?

The Hashtable class discussed above provides constructors for creating Hashtable objects and methods for adding, removing elements, and checking if the element or key, or value is present in the hashtable. It also provides properties to check if the hashtable is empty, counting the number of elements in the hashtable, etc.

But it does not allow our methods to replace items in the entire hashtable from another hashtable. We can replace individual items by replacing their values.

To replace the entire hashtable content with that of another hashtable, we usually traverse the entire second hashtable and replace the content of the first hashtable with that of the second hashtable.

We will use the approach shown below.

foreach (DictionaryEntry item in secondHashtable) {
   firstHashtable[item.Key] = item.Value;
   Console.WriteLine("{0} ({1}) ", item.Key, item.Value);
}

Here first we traverse through the second hashtable and then replace each key-value pair of the first hashtable with the second one.

Example

The entire program that implements this approach is given below.

using System;
using System. Collections;
class MyHashTable {
   // Main Method
   static public void Main() {
      // Create hashtable using the default constructor
      Hashtable indianNumberSystem = new Hashtable();
     
      //add a key/value pair using the Add() method
      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 langCodes){ Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value); } Console.WriteLine("
After Replacing with langCodes, indianNumberSystem: "); foreach (DictionaryEntry item in langCodes) { indianNumberSystem[item.Key] = item.Value; Console.WriteLine("{0} ({1}) ", item.Key, item.Value); } } }

Here we have two hashtables, indianNumberSystem and langCodes. We populate both hashtables with values and then display their contents. Then we traverse the langCodes hashtable and replace each element of the indianNumberSystem hashtable with that of the langCodes hashtable.

The output of this program is shown below.

Output

Contents of indianNumberSystem hashtable:
1000 (Thousand) 
10 (Tens) 
100 (Hundred) 
1 (Ones) 
Contents of langCodes Hashtable:
Java (Java) 
C# (CSharp) 
PL (Perl) 
C++ (CPlusPlus) 
After Replacing with langCodes, indianNumberSystem: 
Java (Java) 
C# (CSharp) 
PL (Perl) 
C++ (CPlusPlus) 

From the output, we can see that after replacing the contents of indianNumberSystem are replaced by the contents of langCodes.

Example

Let’s see the next example now.

In this example again we will have two hashtables, indianNumberSystem and numSys. Here we don’t populate the hashtable indianNumberSystem. We just create an object. The numSys hashtable has the following values added to it using the Add method.

1

Ones

10

Tens

100

Hundred

1000

Thousand

The complete program for this example is shown below.

using System;
using System. Collections;
class MyHashTable {
   // Main Method
   static public void Main() {
      // Create hashtable using the default constructor
      Hashtable indianNumberSystem = new Hashtable();
      Hashtable numSys = new Hashtable();
      numSys.Add(1,"Ones");
      numSys.Add(10,"Tens");
      numSys.Add(100,"Hundred");
      numSys.Add(1000,"Thousand");
      Console.WriteLine("
Contents of numSys Hashtable:"); foreach(DictionaryEntry ele1 in numSys){ Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value); } Console.WriteLine("
After Replacing with numSys, indianNumberSystem: "); foreach (DictionaryEntry item in numSys) { indianNumberSystem[item.Key] = item.Value; Console.WriteLine("{0} ({1}) ", item.Key, item.Value); } } }

Here we use the same approach as in the first program with the only difference that the first hashtable is empty. Then we directly replace or move the items of the second hashtable into the first hashtable.

Output

The output of this program is given below.

Contents of numSys Hashtable:
1000 (Thousand)
10 (Tens)
100 (Hundred)
1 (Ones)
After Replacing with numSys, indianNumberSystem:
1000 (Thousand)
10 (Tens)
100 (Hundred)
1 (Ones)

As the output of the program depicts, the contents of the numSys table are now the contents of the indianNumberSystem.

Thus using a simple loop and by traversing the hashtable we can replace the items of one hashtable with another hashtable.

Updated on: 22-Dec-2022

267 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements