1. 程式人生 > >C# 判斷兩個集合(List)是否相等

C# 判斷兩個集合(List)是否相等

getc tcollect color 是否 rep 復雜度 etl blog 重復項

1.兩個list如果有重復元素(如List1: a,b,a List2: b,b,a) 是無法通過包含關系來判斷是否相等的.

有兩個辦法,其一是兩個List排序後再按順序比較.另一個辦法就是計算各元素的重復項再進行比較

第一種方案劣勢太明顯,時間復雜度過大

第二種以空間換時間,只需要遍歷無需排序即可.

        /// <summary>
        /// 判斷兩個集合是否是相等的(所有的元素及數量都相等)
        /// </summary>
        /// <typeparam name="T">集合元素類型</typeparam>
        ///
<param name="sourceCollection">源集合列表</param> /// <param name="targetCollection">目標集合列表</param> /// <returns>兩個集合相等則返回True,否則返回False</returns> public static bool EqualList<T>(this IList<T> sourceCollection, IList<T> targetCollection) where
T : IEquatable<T> { //空集合直接返回False,即使是兩個都是空集合,也返回False if (sourceCollection == null || targetCollection == null) { return false; } if (object.ReferenceEquals(sourceCollection, targetCollection)) {
return true; } if (sourceCollection.Count != targetCollection.Count) { return false; } var sourceCollectionStaticsDict = sourceCollection.StatisticRepetition(); var targetCollectionStaticsDict = targetCollection.StatisticRepetition(); return sourceCollectionStaticsDict.EqualDictionary(targetCollectionStaticsDict); } /// <summary> /// 判斷兩個字典是否是相等的(所有的字典項對應的值都相等) /// </summary> /// <typeparam name="TKey">字典項類型</typeparam> /// <typeparam name="TValue">字典值類型</typeparam> /// <param name="sourceDictionary">源字典</param> /// <param name="targetDictionary">目標字典</param> /// <returns>兩個字典相等則返回True,否則返回False</returns> public static bool EqualDictionary<TKey, TValue>(this Dictionary<TKey, TValue> sourceDictionary, Dictionary<TKey, TValue> targetDictionary) where TKey : IEquatable<TKey> where TValue : IEquatable<TValue> { //空字典直接返回False,即使是兩個都是空字典,也返回False if (sourceDictionary == null || targetDictionary == null) { return false; } if (object.ReferenceEquals(sourceDictionary, targetDictionary)) { return true; } if (sourceDictionary.Count != targetDictionary.Count) { return false; } //比較兩個字典的Key與Value foreach (var item in sourceDictionary) { //如果目標字典不包含源字典任意一項,則不相等 if (!targetDictionary.ContainsKey(item.Key)) { return false; } //如果同一個字典項的值不相等,則不相等 if (!targetDictionary[item.Key].Equals(item.Value)) { return false; } } return true; } /// <summary> /// 統計集合的重復項,並返回一個字典 /// </summary> /// <typeparam name="T">集合元素類型</typeparam> /// <param name="sourceCollection">統計集合列表</param> /// <returns>返回一個集合元素及重復數量的字典</returns> private static Dictionary<T, int> StatisticRepetition<T>(this IEnumerable<T> sourceCollection) where T : IEquatable<T> { var collectionStaticsDict = new Dictionary<T, int>(); foreach (var item in sourceCollection) { if (collectionStaticsDict.ContainsKey(item)) { collectionStaticsDict[item]++; } else { collectionStaticsDict.Add(item, 1); } } return collectionStaticsDict; }

2

 public class CommonTest  
    {
        /// <summary>
        /// 集合相等比較
        /// </summary>
        [Fact]
        public void ListEqual_Tests()
        {
            var sourceList = new List<string>()
            {
                "a",
                "b",
                "a"
            };

            var targetList = new List<string>()
            {
                "b",
                "b",
                "a"
            };

            var resp = sourceList.EqualList(targetList);
            Assert.False(resp );
        }

        /// <summary>
        /// 集合相等比較
        /// </summary>
        [Fact]
        public void ListEqual2_Tests()
        {
            var sourceList = new List<string>()
            {
                "a",
                "b",
            };

            var targetList = new List<string>()
            {
                "b",
                "a"
            };

            var resp = sourceList.EqualList(targetList);
            Assert.True(resp);
        }
    }

C# 判斷兩個集合(List)是否相等