C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#であるハッシュテーブル(Hashtable)が別のハッシュテーブルと等しいかどうかを確認する方法

C#で、あるHashtable(ハッシュテーブル)が別のHashtableと等しいかどうかを確認するには、Equals()メソッドを使用します。このメソッドは、両方のハッシュテーブルに格納されているキーと値のペアが完全に一致する場合に true を返し、一つでも異なる要素があれば false を返します。

例1:内容が異なるハッシュテーブルの比較

まず、2つのハッシュテーブルを作成し、それぞれに異なるデータを追加して比較してみましょう。

using System;
using System.Collections;
public class Demo {
   public static void Main(){
      Hashtable hash1 = new Hashtable();
      hash1.Add("1", "Kevin");
      hash1.Add("2", "Steve");
      hash1.Add("3", "Tim");
      hash1.Add("4", "Gary");
      hash1.Add("5", "Kevin");
      hash1.Add("6", "Steve");
      hash1.Add("7", "Tom");
      hash1.Add("8", "Stephen");
      Console.WriteLine("HashSet1...");
      ICollection key = hash1.Keys;
      foreach (string k in key) {
         Console.WriteLine(k + ": " + hash1[k]);
      }
      Hashtable hash2 = new Hashtable();
      hash2.Add("1", "Kevin");
      hash2.Add("2", "Steve");
      hash2.Add("3", "John");
      hash2.Add("4", "Tim");
      key = hash2.Keys;
      Console.WriteLine("\nHashSet2...");
      foreach (string k in key) {
         Console.WriteLine(k + ": " + hash2[k]);
      }
      Console.WriteLine("\nAre both the Hashtable equal? " + (hash1.Equals(hash2)));
   }
}

出力

上記のコードを実行すると、次の出力が得られます。

HashSet1...
1: Kevin
2: Steve
3: Tim
4: Gary
5: Kevin
6: Steve
7: Tom
8: Stephen
HashSet2...
1: Kevin
2: Steve
3: John
4: Tim
Are both the Hashtable equal? False

この例では、hash1hash2 の要素数や値が異なるため、比較結果は False になっています。

例2:要素数が異なるハッシュテーブルの比較

続いて、別のサンプルコードを見てみましょう。

using System;
using System.Collections;
public class Demo {
   public static void Main(){
      Hashtable hash1 = new Hashtable();
      hash1.Add("1", "Kevin");
      hash1.Add("2", "Steve");
      hash1.Add("3", "John");
      hash1.Add("4", "Tim");
      Console.WriteLine("HashSet1...");
      ICollection key = hash1.Keys;
      foreach (string k in key) {
         Console.WriteLine(k + ": " + hash1[k]);
      }
      Hashtable hash2 = new Hashtable();
      hash2.Add("1", "Nathan");
      hash2.Add("2", "Gary");
      hash2.Add("3", "John");
      hash2.Add("4", "Tim");
      hash2.Add("5", "Steve");
      ICollection key2 = hash2.Keys;
      Console.WriteLine("\nHashSet2...");
      foreach (string k in key2) {
         Console.WriteLine(k + ": " + hash2[k]);
      }
      Console.WriteLine("\nAre both the Hashtable equal? " + (hash1.Equals(hash2)));
   }
}

出力

上記のコードを実行すると、次の出力が得られます。

HashSet1...
1: Kevin
2: Steve
3: John
4: Tim
HashSet2...
1: Nathan
2: Gary
3: John
4: Tim
5: Steve
Are both the Hashtable equal? False

ポイントまとめ

  • Equals() メソッドを使うことで、2つの Hashtable が等しいかどうかを簡単に判定できます。
  • キーと値のペアがすべて一致している場合のみ true が返されます。
  • 要素数が異なる場合や、同じキーに対して値が異なる場合は false が返されます。
  1. C#でHashtableが固定サイズかどうかを確認する方法

    C# の Hashtable(ハッシュテーブル)が固定サイズを持っているかどうかを確認するには、IsFixedSize プロパティを使用します。このプロパティは、コレクションが固定サイズの場合に true、要素の追加や削除が可能な可変サイズの場合に false を返します。通常、new Hashtable() で作成した Hashtable は動的にサイズが変更されるため、false が返されます。例1:IsFixedSizeプロパティの基本的な使い方以下のコードでは、初期容量 10 の Hashtable を作成し、複数のキーと値を追加したうえで、IsFixedSize プロパティの値を出力

  2. C#で配列オブジェクトが別の配列オブジェクトと等しいかどうかを確認する方法

    C#である配列オブジェクトが別の配列オブジェクトと等しいかどうかを確認するには、Equals()メソッドを使用します。なお、このメソッドは配列の要素内容ではなく、参照(同じオブジェクトを指しているかどうか)を比較する点に注意が必要です。例1:異なる配列オブジェクトを比較するまず、2つの独立した文字列配列をEquals()メソッドで比較してみましょう。using System; public class Demo {    public static void Main(){       String[]