找出两个ICollection< T>集合包含相同的对象 [英] Fastest way to find out whether two ICollection<T> collections contain the same objects

查看:244
本文介绍了找出两个ICollection< T>集合包含相同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找出两个 ICollection< T> 集合是否包含完全相同的条目的最快方法是什么?暴力是清楚的,我想知道是否有一个更优雅的方法。



我们使用C#2.0,所以没有可能的扩展方法!

编辑:对于有序和无序的集合,答案将会很有趣,并且希望每个集合都有所不同。

解决方案

使用C5



http://www.itu.dk/research/c5/



ContainsAll


检查$ b中的所有项目$ b提供的集合在此包

(计算多重性)。

要查找的
项目。



如果所有项目都是
,则为true。




  [测试] 

public virtual bool ContainsAll< U>(SCG.IEnumerable< U>项目)其中U:T
{
HashBag< T> res = new HashBag< T>(itemequalityComparer);

foreach(T item in items)
if(res.ContainsCount(item)< ContainsCount(item))
res.Add(item);
else
return false;

return true;
}


What is the fastest way to find out whether two ICollection<T> collections contain precisely the same entries? Brute force is clear, I was wondering if there is a more elegant method.

We are using C# 2.0, so no extension methods if possible, please!

Edit: the answer would be interesting both for ordered and unordered collections, and would hopefully be different for each.

解决方案

use C5

http://www.itu.dk/research/c5/

ContainsAll

" Check if all items in a supplied collection is in this bag
(counting multiplicities).
The items to look for.

True if all items are found."

[Tested]

public virtual bool ContainsAll<U>(SCG.IEnumerable<U> items) where U : T
{
  HashBag<T> res = new HashBag<T>(itemequalityComparer);

  foreach (T item in items)
    if (res.ContainsCount(item) < ContainsCount(item))
      res.Add(item);
    else
      return false;

  return true;
}

这篇关于找出两个ICollection&lt; T&gt;集合包含相同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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