Comparing two collections for equality irrespective of the order of items in them

后端 未结 19 2130
我在风中等你
我在风中等你 2020-11-22 10:28

I would like to compare two collections (in C#), but I\'m not sure of the best way to implement this efficiently.

I\'ve read the other thread about Enumerable.Sequen

19条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 11:11

    This is my (heavily influenced by D.Jennings) generic implementation of the comparison method (in C#):

    /// 
    /// Represents a service used to compare two collections for equality.
    /// 
    /// The type of the items in the collections.
    public class CollectionComparer
    {
        /// 
        /// Compares the content of two collections for equality.
        /// 
        /// The first collection.
        /// The second collection.
        /// True if both collections have the same content, false otherwise.
        public bool Execute(ICollection foo, ICollection bar)
        {
            // Declare a dictionary to count the occurence of the items in the collection
            Dictionary itemCounts = new Dictionary();
    
            // Increase the count for each occurence of the item in the first collection
            foreach (T item in foo)
            {
                if (itemCounts.ContainsKey(item))
                {
                    itemCounts[item]++;
                }
                else
                {
                    itemCounts[item] = 1;
                }
            }
    
            // Wrap the keys in a searchable list
            List keys = new List(itemCounts.Keys);
    
            // Decrease the count for each occurence of the item in the second collection
            foreach (T item in bar)
            {
                // Try to find a key for the item
                // The keys of a dictionary are compared by reference, so we have to
                // find the original key that is equivalent to the "item"
                // You may want to override ".Equals" to define what it means for
                // two "T" objects to be equal
                T key = keys.Find(
                    delegate(T listKey)
                    {
                        return listKey.Equals(item);
                    });
    
                // Check if a key was found
                if(key != null)
                {
                    itemCounts[key]--;
                }
                else
                {
                    // There was no occurence of this item in the first collection, thus the collections are not equal
                    return false;
                }
            }
    
            // The count of each item should be 0 if the contents of the collections are equal
            foreach (int value in itemCounts.Values)
            {
                if (value != 0)
                {
                    return false;
                }
            }
    
            // The collections are equal
            return true;
        }
    }
    

提交回复
热议问题