zoukankan      html  css  js  c++  java
  • C#中相等性判断

    我们在想对一个可枚举的对象集合进行去重操作时,一般第一个想到的就是就是Linq的Distinct方法。
    先定义一个类,然后使用Distinct方法去重。

    class Man
        {
          public int Age { get; set; }
          public string Name { get; set; }
          public string Adress { get; set; }
          public decimal Weight { get; set; }
          public decimal Height { get; set; }
        }
    
    List<Man> list = new List<Man>()
          { 
          new Man(){Age=21,Name="Adam",Adress="Shenzhen",Weight=60,Height=170},
          new Man(){Age=21,Name="Adam",Adress="Shenzhen",Weight=60,Height=170}
          };
          var distinct = list.Distinct();
    

    然而去重得到的distinct集合的Count依然为二,集合里依然存在两个Adam。
    实际上,Distinct方法内进行比较的是声明的引用,而不是对象属性,就和对两个属性一模一样的对象使用Equals()方法得到的是False一样。
    因此我们对对象集合使用Distinct方法时要使用重载Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer);
    要使用这个方法,我们得重写IEqualityComparer接口,再使用Distinct方法:

    public class ManComparer : IEqualityComparer<Man>
        {
          public bool Equals(Man x, Man y)
          {
            return x.Age == y.Age
              && x.Name == y.Name
              && x.Adress == y.Adress
              && x.Weight == y.Weight
              && x.Height == y.Height;
          }
     
          public int GetHashCode(Man obj)
          {
            return obj.GetHashCode();
          }
        }
     
    var distinct = list.Distinct(new ManComparer());
    

    然而,再一次,distinct集合内依然有两个对象。
    实际上,由于直接获取对象的HashCode,用HashCode进行比较的速度比 Equals 方法更快,因此IEqualityComparer内部会在使用 Equals 前先使用 GetHashCode 方法,在两个对象的HashCode都相同时即刻判断对象相等。而当两个对象HashCode不相同时, Equals 方法就会被调用,对要比较的对象进行判断。
    由于在上例中list中的两个引用实际上是两个不同的对象,因此HashCode必定不相同,所以要触发Equlas方法,我们需要改 GetHashCode ,让它返回相同的常量.

    public class ManComparerNew : IEqualityComparer<Man>
        {
          public bool Equals(Man x, Man y)
          {
            return x.Age == y.Age
              && x.Name == y.Name
              && x.Adress == y.Adress
              && x.Weight == y.Weight
              && x.Height == y.Height;
          }
     
          public int GetHashCode(Man obj)
          {
            return 1;
          }
        }
     
    var distinct = list.Distinct(new ManComparerNew());
    

    现在distinct集合中就只有一个Man对象了,成功实现了去重。

    参考链接:
    https://www.jb51.net/article/162602.htm
    https://www.cnblogs.com/oberon-zjt0806/p/12355028.html#最后的故事真实印记

  • 相关阅读:
    概率与数学期望
    UVa10870
    UVa10870
    特殊符号+运算优先级
    特殊符号+运算优先级
    poj 2065 SETI(gauss--->≡)
    poj 2065 SETI(gauss--->≡)
    线性筛法证明
    HDU 1728 逃离迷宫
    T^T ONLINE JUDGE 2401 尼克的任务
  • 原文地址:https://www.cnblogs.com/chendeqiang/p/12861604.html
Copyright © 2011-2022 走看看