简体   繁体   English

比较两个列表的最快方法<CustomObject>

[英]Fastest way to compare two List<CustomObject>

I have two List<CustomObject> , called list1 and list2 我有两个List<CustomObject> ,分别称为list1和list2

public class CustomObject
{
    public string foo { get; set; }
    public string bar{ get; set; }
}

The goal is to generate a new list with all the entries that have been modified/added in list2. 目标是生成一个新列表,其中包含在list2中已修改/添加的所有条目。

Because these lists can get pretty long, looping through them is not an option ... 由于这些列表可能会很长,因此无法遍历它们...

Any ideas? 有任何想法吗?

Here's a traditional way to do it: 这是一种传统的方式:

public class CustomObject : IComparable
{
    public string foo { get; set; }
    public string bar{ get; set; }
    public int CompareTo(CustomObject o)
    {
         if (this.foo == o.foo && this.bar == o.bar) return 0;

         //We have to code for the < and > comparisons too.  Could get painful if there are a lot of properties to compare.
         if (this.Foo == o.Foo) return (this.Bar.CompareTo(o.Bar));
         return this.Foo.CompareTo(o.Foo);
    }
}

Then use Linq.Except : 然后使用Linq.Except

listA.Except(listB)

Adding another answer to accomodate some additional NFRs that have come up in the comments: 添加另一个答案以适应注释中出现的一些其他NFR:

  1. Objects can be identified by a hash code 可以通过哈希码识别对象
  2. The list is very big, so performance is an issue 该列表很大,因此性能是一个问题
  3. The idea is to compare an old list to a new list to see if any new hash codes have popped up. 想法是将旧列表与新列表进行比较,以查看是否弹出了任何新的哈希码。

You will want to store your objects in a dictionary: 您将需要将对象存储在字典中:

var list = new Dictionary<string, CustomObject>();

When you add them, provide the hash as the key: 添加它们时,请提供哈希作为键:

list.Add(customObject.Hash, customObject);

To scan for new ones: 要扫描新的:

var difference = new List<CustomObject>();
foreach (customObject o in newList)
{
    if (oldList.ContainsKey(o.Hash)) difference.Add(o);
}
Log(String.Format("{0} new hashes found.", difference.Count));

By using the Dictionary you take advantage of the way the keys are stored in a hash table. 通过使用字典,您可以利用密钥存储在哈希表中的方式。 Finding an item in a hash table is faster than just doing a scan & compare sort of thing. 在哈希表中查找项目比仅执行扫描和比较之类的事情要快。 I believe this will be O(n*log(n)) instead of O(n^2). 我相信这将是O(n * log(n))而不是O(n ^ 2)。

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

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