List的并集、交集、差集

并集:List<xx> union =arps.Union(flts).ToList();
交集:List<xx> inters = arps.Intersect(flts)ToList();
差集:List<xx> except= arps.Except(flts)ToList();

补充:

如果要自定义两个对象认为相等的条件,需补充compary<>参数。如:

    VOR v1 = new VOR("V1", "ZBAA", "ZY");
            VOR v2 = new VOR("V2", "ZBAA", "ZB");
            VOR v3 = new VOR("V3", "ZUUU", "ZU");
            VOR v4 = new VOR("V4", "ZGGG", "ZG");
            VOR v5 = new VOR("V5", "ZSSS", "ZS");
            VOR v6 = new VOR("V6", "ZUUU", "ZU");

 

    List<VOR> a = new List<VOR>();

            a.Add(v1);             a.Add(v2);             a.Add(v3);

            List<VOR> b = new List<VOR>();

            b.Add(v4);             b.Add(v5);            b.Add(v6);

           

    List<VOR> jiao = a.Intersect(b, new MyComPare()).ToList(); //在有比较函数情况下返回 v3,无比较函数时返回空

            List<VOR> bin = a.Union(b, new MyComPare()).ToList();     //在有比较函数情况下返回 v1~v5(v3和v6认为相等),无比较函数时返回v1~v6

            List<VOR> ca = a.Except(b, new MyComPare()).ToList();    //在有比较函数情况下返回 v1、v2(v3和v6认为相等),无比较函数时返回v1、v2、v3

 

//自定义比较函数

public class MyComPare : IEqualityComparer<VOR>        

{

//重点是此相等条件和哈希函数的理解             

// Euquals用于确定两对象认为相等的条件,相关的Intersect、Union、Execpt等函数都是根据此函数返回的bool值判断两对象是否相等

//但是,这两个对象还必须保证哈希函数返回值也相等才能最终认为相等

//也就是说要同时满足这两个函数条件才能认为相等,缺一不可

//一般可以设置 哈希返回值就是对象.ToString(),保证比较的两个对象是同一类型的就可满足此哈希条件。这样就只用考虑Equals函数

 

    public bool Equals(VOR x, VOR y) //此处认为若两vor的后两个属性相同就相等。

    {

              return x.fir+x.icd==y.fir+y.icd;                     

    }           

           public int GetHashCode(VOR n) 

             {

                 return n.ToString().GetHashCode();

             }        

}

 

//如果List成员时数组,比较函数应类似

public class ArrayComPare : IEqualityComparer<string[]>

        {

            public bool Equals(string[] x, string[] y)

            {

                if (x.Length != y.Length) return false;

 

                //这里可以灵活设置两个元素的比较条件,如支持不对最后一位数组比较等

                for (int i = 0; i < x.Length; i++)

                    if (x[i] != y[i]) return false;

 

                return true;

 

            }

 

            public int GetHashCode(string[] n)

            {

                return n.ToString().GetHashCode(); //对数组类的比较,一定要Tostring()后再去哈希值,否则哈希值不同,两个对象始终会比较不同。

            }

        }

 

 

 

 

关于List的交集和并集的东西,上代码

复制代码
  class Program
    {
        static void Main(string[] args)
        {
            List<Fish> a = new List<Fish>();
            a.Add(new Fish() {Name="测试1",ID=0,Message="" });
            a.Add(new Fish() { Name = "测试2", ID = 1, Message = "" });
            a.Add(new Fish() { Name = "测试3", ID = 2, Message = "" });
            a.Add(new Fish() { Name = "测试4", ID = 3, Message = "" });
            List<Fish> b = new List<Fish>();
            b.Add(new Fish() { Name = "测试1", ID = 0, Message = "" });
            b.Add(new Fish() { Name = "测试3", ID = 5, Message = "" });
            b.Add(new Fish() { Name = "测试5", ID = 6, Message = "" });


            List<Fish> c = a.Union(b).ToList();             //List<Fish> c = a.Intersect(b).ToList();
            foreach (var item in c)
            {
                Console.WriteLine("Name:{0},ID:{1}", item.Name, item.ID);
            }
            Console.ReadLine();
        }

    }

    public class Fish
    {
        public string Name { get; set; }
        public int ID { get; set; }
        public string Message { get; set; }
    }
复制代码

一个简单的测试类有三个属性,现在我想取a和b的并集,结果如下:

结果并不是我想要的,“测试1”这个结果应该显示一次就够了,而 List<Fish> c = a.Intersect(b).ToList();显示的结果更是空白,这是怎么回事儿? 来看一下Union的原型

复制代码
        //
        // 摘要:
        //     通过使用默认的相等比较器生成两个序列的并集。
        //
        // 参数:
        //   first:
        //     一个 System.Collections.Generic.IEnumerable<T>,它的非重复元素构成联合的第一个集。
        //
        //   second:
        //     一个 System.Collections.Generic.IEnumerable<T>,它的非重复元素构成联合的第二个集。
        //
        // 类型参数:
        //   TSource:
        //     输入序列中的元素的类型。
        //
        // 返回结果:
        //     一个 System.Collections.Generic.IEnumerable<T>,包含两个输入序列中的元素(重复元素除外)。
        //
        // 异常:
        //   System.ArgumentNullException:
        //     first 或 second 为 null。
        public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
        //
        // 摘要:
        //     通过使用指定的 System.Collections.Generic.IEqualityComparer<T> 生成两个序列的并集。
        //
        // 参数:
        //   first:
        //     一个 System.Collections.Generic.IEnumerable<T>,它的非重复元素构成联合的第一个集。
        //
        //   second:
        //     一个 System.Collections.Generic.IEnumerable<T>,它的非重复元素构成联合的第二个集。
        //
        //   comparer:
        //     用于对值进行比较的 System.Collections.Generic.IEqualityComparer<T>。
        //
        // 类型参数:
        //   TSource:
        //     输入序列中的元素的类型。
        //
        // 返回结果:
        //     一个 System.Collections.Generic.IEnumerable<T>,包含两个输入序列中的元素(重复元素除外)。
        //
        // 异常:
        //   System.ArgumentNullException:
        //     first 或 second 为 null。
        public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);
复制代码

还有一个重载函数,还有一个比较器 那咱们就写一个比较器呗

复制代码
    public class FishComPare : IEqualityComparer<Fish>
    {

        public bool Equals(Fish x, Fish y)
        {
            return x.Name == y.Name && x.ID==y.ID;
        }

        public int GetHashCode(Fish obj)
        {
            return obj.Name.GetHashCode();
        }
    }
复制代码

然后全部代码

复制代码
   class Program
    {
        static void Main(string[] args)
        {
            List<Fish> a = new List<Fish>();
            a.Add(new Fish() { Name = "测试1", ID = 0, Message = "" });
            a.Add(new Fish() { Name = "测试2", ID = 1, Message = "" });
            a.Add(new Fish() { Name = "测试3", ID = 2, Message = "" });
            a.Add(new Fish() { Name = "测试4", ID = 3, Message = "" });
            List<Fish> b = new List<Fish>();
            b.Add(new Fish() { Name = "测试1", ID = 0, Message = "" });
            b.Add(new Fish() { Name = "测试3", ID = 5, Message = "" });
            b.Add(new Fish() { Name = "测试5", ID = 6, Message = "" });


              List<Fish> c = a.Union(b,new FishComPare()).ToList();
              //List<Fish> c = a.Intersect(b,,new FishComPare()).ToList();
            foreach (var item in c)
            {
                Console.WriteLine("Name:{0},ID:{1}", item.Name, item.ID);
            }
            Console.ReadLine();
        }

    }

    public class Fish
    {
        public string Name { get; set; }
        public int ID { get; set; }
        public string Message { get; set; }
    }

    public class FishComPare : IEqualityComparer<Fish>
    {

        public bool Equals(Fish x, Fish y)
        {
            return x.Name == y.Name && x.ID==y.ID;
        }

        public int GetHashCode(Fish obj)
        {
            return obj.Name.GetHashCode();
        }
    }

posted on 2016-11-08 11:19  mol1995  阅读(3695)  评论(0编辑  收藏  举报

导航