简体   繁体   English

比较两个列表的最快方法

[英]Fastest way to compare two lists

I have a List (Foo) and I want to see if it's equal to another List (foo). 我有一个List(Foo),我想看看它是否等于另一个List(foo)。 What is the fastest way ? 什么是最快的方式?

From 3.5 onwards you may use a LINQ function for this: 从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));

It also knows an overload to provide your own comparer 它也知道提供自己的比较器的过载

Here are the steps I would do: 以下是我要做的步骤:

  1. Do an object.ReferenceEquals() if true, then return true. 如果为true,则执行object.ReferenceEquals(),然后返回true。
  2. Check the count, if not the same, return false. 检查计数,如果不相同,则返回false。
  3. Compare the elements one by one. 逐个比较元素。

Here are some suggestions for the method: 以下是该方法的一些建议:

  1. Base the implementation on ICollection. 将实现基于ICollection。 This gives you the count, but doesn't restrict to specific collection type or contained type. 这为您提供了计数,但不限于特定的集合类型或包含的类型。
  2. You can implement the method as an extension method to ICollection. 您可以将该方法实现为ICollection的扩展方法。
  3. You will need to use the .Equals() for comparing the elements of the list. 您将需要使用.Equals()来比较列表的元素。

Something like this: 像这样的东西:

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;
}

Some additional error checking (eg null-checks) might be required. 可能需要一些额外的错误检查(例如,空检查)。

One shortcut, that I didn't see mentioned, is that if you know how the lists were created, you may be able to join them into strings and compare directly. 我没有看到的一个捷径是,如果您知道如何创建列表,您可以将它们连接成字符串并直接进行比较。

For example... 例如...

In my case, I wanted to prompt the user for a list of words. 就我而言,我想提示用户输入单词列表。 I wanted to make sure that each word started with a letter, but after that, it could contain letters, numbers, or underscores. 我想确保每个单词以字母开头,但在此之后,它可能包含字母,数字或下划线。 I'm particularly concerned that users will use dashes or start with numbers. 我特别担心用户会使用破折号或以数字开头。

I use Regular Expressions to break it into 2 lists, and them join them back together and compare them as strings: 我使用正则表达式将它分成2个列表,然后将它们连接在一起并将它们作为字符串进行比较:

    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"

Since I knew that neither list would contain spaces, and that the lists only contained simple strings, I could join them together with a space, and compare them. 因为我知道两个列表都不包含空格,并且列表只包含简单的字符串,所以我可以将它们与空格连接在一起,并进行比较。

Something like this maybe using Match Action. 这样的事情可能使用Match Action。

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;
   }
}

Assuming you mean that you want to know if the CONTENTS are equal (not just the list's object reference.) 假设你想要知道CONTENTS是否相等(不仅仅是列表的对象引用)。

If you will be doing the equality check much more often than inserts then you may find it more efficient to generate a hashcode each time a value is inserted and compare hashcodes when doing the equality check. 如果您将比插入更频繁地进行相等性检查,那么您可能会发现每次插入值时生成哈希码更有效,并在执行相等性检查时比较哈希码。 Note that you should consider if order is important or just that the lists have identical contents in any order. 请注意,您应该考虑订单是否重要,或者只是列表中的任何顺序具有相同的内容。

Unless you are comparing very often I think this would usually be a waste. 除非你经常比较,否则我认为这通常是浪费。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM