檢查兩個 SortedDictionary 物件在 C# 中是否相等


C# 中的 SortedDictionary 是基於二叉樹的實現,它按鍵的順序維護其元素。它是一組鍵/值對,這些鍵/值對根據鍵進行排序。本文將逐步指導您如何檢查 C# 中的兩個 SortedDictionary 物件是否相等。最終,您將能夠熟練地確定兩個 SortedDictionary 物件是否包含相同的元素並且相等。

理解 C# 中的 SortedDictionary

在繼續之前,務必瞭解 SortedDictionary 是什麼。它是 C# 中基於二叉樹的集合,它按鍵的排序順序儲存鍵值對。它是 System.Collections.Generic 名稱空間的一部分。

這是一個 SortedDictionary 的示例:

SortedDictionary<string, int> sortedDict = new SortedDictionary<string, int>() {
   {"One", 1},
   {"Two", 2},
   {"Three", 3}
};

使用 SequenceEqual 方法比較兩個 SortedDictionary 物件

檢查兩個 SortedDictionary 物件是否相等的最簡單方法之一是使用 System.Linq 名稱空間中的 SequenceEqual 方法。由於 SortedDictionary 會自動根據鍵維護元素的順序,因此您可以直接使用 SequenceEqual 來比較它們。

示例

這是一個演示此方法的程式碼片段:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program {
   public static void Main() {
      SortedDictionary<string, int> sortedDict1 = new SortedDictionary<string, int>() {
         {"One", 1},
         {"Two", 2},
         {"Three", 3}
      };

      SortedDictionary<string, int> sortedDict2 = new SortedDictionary<string, int>() {
         {"One", 1},
         {"Two", 2},
         {"Three", 3}
      };

      bool areEqual = sortedDict1.SequenceEqual(sortedDict2);
      
      Console.WriteLine("SortedDictionaries equal: " + areEqual);
   }
}

輸出

SortedDictionaries equal: True

使用自定義 EqualityComparer

如果您想根據值而不是鍵來比較 SortedDictionaries,或者如果您想實現複雜的比較邏輯,可以使用自定義 EqualityComparer。

示例

以下是實現方法:

using System;
using System.Collections.Generic;
using System.Linq;

class DictionaryComparer<TKey, TValue> : IEqualityComparer<SortedDictionary<TKey, TValue>> {
   public bool Equals(SortedDictionary<TKey, TValue> x, SortedDictionary<TKey, TValue> y) {
      // Check whether the dictionaries are equal
      return x.Count == y.Count && !x.Except(y).Any();
   }

   public int GetHashCode(SortedDictionary<TKey, TValue> obj) {
      int hash = 0;
      foreach (var pair in obj) {
         hash ^= pair.GetHashCode();
      }
      return hash;
   }
}

public class Program {
   public static void Main() {
      SortedDictionary<string, int> sortedDict1 = new SortedDictionary<string, int>() {
         {"One", 1},
         {"Two", 2},
         {"Three", 3}
      };

      SortedDictionary<string, int> sortedDict2 = new SortedDictionary<string, int>() {
         {"One", 1},
         {"Two", 2},
         {"Three", 3}
      };
      
      DictionaryComparer<string, int> comparer = new DictionaryComparer<string, int>();
      bool areEqual = comparer.Equals(sortedDict1, sortedDict2);

      Console.WriteLine("SortedDictionaries equal: " + areEqual);
   }
}

在這個例子中,我們建立了一個自定義相等比較器,它實現了 IEqualityComparer 介面。Equals 方法檢查兩個 SortedDictionaries 是否具有相同數量的元素,以及第一個 SortedDictionary 中是否存在任何不在第二個 SortedDictionary 中的元素。

輸出

SortedDictionaries equal: True

結論

在 C# 中,您可以使用 SequenceEqual 方法或自定義 EqualityComparer 來檢查兩個 SortedDictionary 物件是否相等。雖然 SequenceEqual 方法快速簡便,但自定義 EqualityComparer 為更復雜的比較需求提供了更靈活的解決方案。此自定義比較器可以根據您的特定需求進行調整,包括基於值或任何其他自定義邏輯的比較。

更新於:2023年7月24日

瀏覽量:135

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告