Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较两个列表的最快方法_C#_Collections - Fatal编程技术网

C# 比较两个列表的最快方法

C# 比较两个列表的最快方法,c#,collections,C#,Collections,我有一个列表(Foo),我想看看它是否等于另一个列表(Foo)。最快的方法是什么?类似这样的方法: public static bool CompareLists(List<int> l1, List<int> l2) { if (l1 == l2) return true; if (l1.Count != l2.Count) return false; for (int i=0; i<l1.Count; i++) if (l

我有一个列表(Foo),我想看看它是否等于另一个列表(Foo)。最快的方法是什么?

类似这样的方法:

public static bool CompareLists(List<int> l1, List<int> l2)
{
    if (l1 == l2) return true;
    if (l1.Count != l2.Count) return false;
    for (int i=0; i<l1.Count; i++)
        if (l1[i] != l2[i]) return false;
    return true;
}
公共静态布尔比较器列表(列表l1、列表l2)
{
如果(l1==l2)返回true;
if(l1.Count!=l2.Count)返回false;

对于(int i=0;i,我将执行以下步骤:

  • 执行object.ReferenceEquals()操作,如果为true,则返回true
  • 检查计数,如果不相同,则返回false
  • 逐个比较这些元素
  • 以下是对该方法的一些建议:

  • 基于ICollection实现。这将提供计数,但不限于特定的集合类型或包含的类型
  • 您可以将该方法实现为ICollection的扩展方法
  • 您需要使用.Equals()来比较列表中的元素

  • 类似这样的东西可能会使用匹配动作

    public static CompareList<T>(IList<T> obj1, IList<T> obj2, Action<T,T> match)
    {
       if (obj1.Count != obj2.Count) return false;
       for (int i = 0; i < obj1.Count; i++)
       {
         if (obj2[i] != null && !match(obj1[i], obj2[i]))
           return false;
       }
    }
    
    公共静态比较列表(ILST obj1、ILST obj2、操作匹配)
    {
    如果(obj1.Count!=obj2.Count)返回false;
    对于(int i=0;i
    从3.5版开始,您可以使用LINQ功能:

    List<string> l1 = new List<string> {"Hello", "World","How","Are","You"};
    List<string> l2 = new List<string> {"Hello","World","How","Are","You"};
    Console.WriteLine(l1.SequenceEqual(l2));
    
    List l1=新列表{“你好”,“世界”,“你好”,“你在吗”,“你”};
    列表l2=新列表{“你好”,“世界”,“你好”,“你在吗”,“你”};
    控制台写入线(l1.SequenceEqual(l2));
    

    它还知道一个重载来提供您自己的比较器

    假设您想知道内容是否相等(而不仅仅是列表的对象引用)

    如果您将比插入更多地进行相等检查,那么您可能发现每次插入一个值时生成hash码更有效率,并且在进行相等检查时比较hash码。请注意,如果顺序是重要的,或者只是列表具有任意顺序的相同内容。


    除非您经常比较,否则我认为这通常是一种浪费。

    我没有提到的一个快捷方式是,如果您知道列表是如何创建的,您可以将它们连接到字符串中并直接进行比较

    例如

    在我的例子中,我想提示用户输入一个单词列表。我想确保每个单词都以字母开头,但在这之后,它可以包含字母、数字或下划线。我特别担心用户会使用破折号或以数字开头

    我使用正则表达式将其分为两个列表,然后将它们重新连接在一起,并将它们作为字符串进行比较:

        var testList = userInput.match(/[-|\w]+/g)  
            /*the above catches common errors: 
             using dash or starting with a numeric*/
        listToUse = userInput.match(/[a-zA-Z]\w*/g)
    
        if (listToUse.join(" ") != testList.join(" ")) {
                    return "the lists don't match"
    

    因为我知道这两个列表都不包含空格,而且列表只包含简单的字符串,所以我可以用空格将它们连接在一起,并对它们进行比较。

    您还可以将其设置为“更通用”,还可以使用“比较器”而不是“!=”。它已经存在IComparer()和Comparison()。您不能使用Action,因为它返回void。很抱歉:IComparer位于。您需要实现一个IEqualityComparer来比较自定义类的列表,但它很简单,此MS页面解释了如何:杰出。我只需要在我的对象上实现IEquatable,因为我只关心对象中可能重复的数据