I have a List (Foo) and I want to see if it's equal to another List (foo). What is the fastest way ?
我有一个List(Foo),我想看看它是否等于另一个List(foo)。什么是最快的方式?
6 个解决方案
#1
Here are the steps I would do:
以下是我要做的步骤:
- Do an object.ReferenceEquals() if true, then return true.
- Check the count, if not the same, return false.
- Compare the elements one by one.
如果为true,则执行object.ReferenceEquals(),然后返回true。
检查计数,如果不相同,则返回false。
逐个比较元素。
Here are some suggestions for the method:
以下是该方法的一些建议:
- Base the implementation on ICollection. This gives you the count, but doesn't restrict to specific collection type or contained type.
- You can implement the method as an extension method to ICollection.
- You will need to use the .Equals() for comparing the elements of the list.
将实现基于ICollection。这为您提供了计数,但不限制特定的集合类型或包含的类型。
您可以将该方法实现为ICollection的扩展方法。
您需要使用.Equals()来比较列表的元素。
#2
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
它也知道提供自己的比较器的过载
#3
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 (e.g. null-checks) might be required.
可能需要一些额外的错误检查(例如,空检查)。
#4
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;
}
}
#5
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.
除非你经常比较,否则我认为这通常是一种浪费。
#6
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.
因为我知道两个列表都不包含空格,并且列表只包含简单的字符串,所以我可以将它们与空格连接在一起,并进行比较。
#1
Here are the steps I would do:
以下是我要做的步骤:
- Do an object.ReferenceEquals() if true, then return true.
- Check the count, if not the same, return false.
- Compare the elements one by one.
如果为true,则执行object.ReferenceEquals(),然后返回true。
检查计数,如果不相同,则返回false。
逐个比较元素。
Here are some suggestions for the method:
以下是该方法的一些建议:
- Base the implementation on ICollection. This gives you the count, but doesn't restrict to specific collection type or contained type.
- You can implement the method as an extension method to ICollection.
- You will need to use the .Equals() for comparing the elements of the list.
将实现基于ICollection。这为您提供了计数,但不限制特定的集合类型或包含的类型。
您可以将该方法实现为ICollection的扩展方法。
您需要使用.Equals()来比较列表的元素。
#2
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
它也知道提供自己的比较器的过载
#3
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 (e.g. null-checks) might be required.
可能需要一些额外的错误检查(例如,空检查)。
#4
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;
}
}
#5
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.
除非你经常比较,否则我认为这通常是一种浪费。
#6
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.
因为我知道两个列表都不包含空格,并且列表只包含简单的字符串,所以我可以将它们与空格连接在一起,并进行比较。