最快的方法来检查,如果两个List< T>相等 [英] Fastest way to check if two List<T> are equal

查看:228
本文介绍了最快的方法来检查,如果两个List< T>相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表

利斯塔<的Emp> 数组listB<的Emp>
均为有1000条记录。

ListA<Emp> and ListB<Emp> both are having 1000 records.

的Emp 是一个对象Employee类。下面是我的员工

Emp is an object of Employee Class. Below is my Employee class

public class Employee
{
    int ID = 0;
    string Name = String.Empty;
    string Dept = String.Empty;
    string Address = String.Empty;
    int Age = 0;
    string Email = String.Empty;
}



我想验证,如果两个列表是相等的。对EMP对象可以被放置在不同的顺序。此外,还有可能是它们具有在两个列表中完全相同的信息数的Emp对象。我必须验证者也。

I want to verify if both the Lists are equal. The Emp objects may be placed in different order. Also, there might be several Emp objects which are having exactly same info in both the list. I have to verify those also.

我试图列表进行排序和比较使用 SequenceEqual

I tried to sort the lists and compared using SequenceEqual

Enumerable.SequenceEqual(ListA.OrderBy(s => s), ListB.OrderBy(s => s)

我收到以下错误

At least one object must implement IComparable.
Exception Stack trace is as below 

   at System.Collections.Comparer.Compare(Object a, Object b)
   at System.Collections.Generic.ObjectComparer`1.Compare(T x, T y)
   at System.Linq.EnumerableSorter`2.CompareKeys(Int32 index1, Int32 index2)
   at System.Linq.EnumerableSorter`1.QuickSort(Int32[] map, Int32 left, Int32 right)
   at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
   at System.Linq.Enumerable.SequenceEqual[TSource](IEnumerable`1 first, IEnumerable`1 second, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.SequenceEqual[TSource](IEnumerable`1 first, IEnumerable`1 second)

我怎样才能实现呢?此外,它会更好,如果你们能提供给我这样做是因为对象的名单中的人数可能增长到1000万的最快方法。 !
感谢您的帮助。

How can I implement this ? Also it will be better if you guys can provide me the fastest way of doing this because the number of objects in List may grow to 10 million. Thanks for your help !

编辑:每一位员工都必须在两个列表,顺序并不重要。但是,如果利斯塔包含相同的Employee对象的5倍(这意味着一些重复的条目),以及数组listB包含Employee对象的4倍,那么利斯塔和数组listB是不相等的。

Every employee must be in both list, order does not matter. But, if ListA contains same employee object 5 times (that means some duplicate entries), and ListB contains the employee object 4 times, then ListA and ListB are not equal.

推荐答案

最好的复杂度为O(N)
按照实现使用HashSet的:

Best complexity is O(N) Following realization with using HashSet:

类具有实现的GetHashCode和等于:

Class with implementation of GetHashCode and Equals:

public class Employee
{
    public int ID = 0;
    public string Name = String.Empty;
    public string Dept = String.Empty;
    public string Address = String.Empty;
    public int Age = 0;
    public string Email = String.Empty;

    public override int GetHashCode()
    {
        return
            ID.GetHashCode() ^
            (Name ?? String.Empty).GetHashCode() ^
            (Dept ?? String.Empty).GetHashCode() ^
            (Address ?? String.Empty).GetHashCode() ^
            Age.GetHashCode() ^
            (Email ?? String.Empty).GetHashCode()
            ;
    }
    public override bool Equals(object obj)
    {
        Employee other = obj as Employee;
        if (obj == null)
            return false;

        return ID == other.ID &&
                Name == other.Name &&
                Dept == other.Dept &&
                Address == other.Address &&
                Age == other.Age &&
                Email == other.Email;
    }
}



功能对比列表:

Function to compare lists:

public static bool CompareLists(List<Employee> list1, List<Employee> list2)
{
    if (list1 == null || list2 == null)
        return list1 == list2;

    if (list1.Count != list2.Count)
        return false;
    Dictionary<Employee, int> hash = new Dictionary<Employee, int>();
    foreach (Employee employee in list1)
    {
        if (hash.ContainsKey(employee))
        {
            hash[employee]++;
        }
        else
        {
            hash.Add(employee, 1);
        }
    }

    foreach (Employee employee in list2)
    {
        if (!hash.ContainsKey(employee) || hash[employee] == 0)
        {
            return false;
        }
        hash[employee]--;
    }

    return true;
}

这篇关于最快的方法来检查,如果两个List&LT; T&GT;相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆