![](/img/trans.png)
[英]Should I implement non-generic GetHashCode and Equals if my class implements IEqualityComparer<T>?
[英]Generic IEqualityComparer<T> and GetHashCode
对于实现大量 IEqualityComparers 有点懒惰,并且考虑到我无法轻松编辑正在比较的对象的类实现,我选择了以下内容,旨在与 Distinct() 和 Except() 扩展方法一起使用。 :
public class GenericEqualityComparer<T> : IEqualityComparer<T>
{
Func<T, T, bool> compareFunction;
Func<T, int> hashFunction;
public GenericEqualityComparer(Func<T, T, bool> compareFunction, Func<T, int> hashFunction)
{
this.compareFunction = compareFunction;
this.hashFunction = hashFunction;
}
public bool Equals(T x, T y)
{
return compareFunction(x, y);
}
public int GetHashCode(T obj)
{
return hashFunction(obj);
}
}
看起来不错,但是每次都真的需要提供一个散列函数吗? 我知道哈希码用于将对象放入桶中。 不同的bucket,object不相等,不调用equal。
如果 GetHashCode 返回相同的值,则调用 equals。 (来自: 为什么在覆盖 Equals 方法时覆盖 GetHashCode 很重要? )
那么会出现什么问题,例如(我听到很多程序员惊恐地尖叫),GetHashCode 返回一个常量,强制调用 Equal ?
什么都不会出错,但是在基于哈希表的容器中,在进行查找时,性能大约为 O(1) 到 O(n)。 您最好简单地将所有内容存储在一个 List 中,然后强力搜索它以查找满足相等性的项目。
如果一个常见的用例是根据对象的一个属性比较对象,您可以添加一个额外的构造函数并像这样实现和调用它:
public GenericEqualityComparer(Func<T, object> projection)
{
compareFunction = (t1, t2) => projection(t1).Equals(projection(t2));
hashFunction = t => projection(t).GetHashCode();
}
var comaparer = new GenericEqualityComparer( o => o.PropertyToCompare);
这将自动使用由属性实现的哈希。
编辑:更有效和更强大的实现激发了我的 Marc 的评论:
public static GenericEqualityComparer<T> Create<TValue>(Func<T, TValue> projection)
{
return new GenericEqualityComparer<T>(
(t1, t2) => EqualityComparer<TValue>.Default.Equals( projection(t1), projection(t2)),
t => EqualityComparer<TValue>.Default.GetHashCode(projection(t)));
}
var comparer = GenericEqualityComparer<YourObjectType>.Create( o => o.PropertyToCompare);
在 CodeProject - A Generic IEqualityComparer for Linq Distinct()上找到了这个,做得很好。
用例:
IEqualityComparer<Contact> c = new PropertyComparer<Contact>("Name");
IEnumerable<Contact> distinctEmails = collection.Distinct(c);
通用 IEqualityComparer
public class PropertyComparer<T> : IEqualityComparer<T>
{
private PropertyInfo _PropertyInfo;
/// <summary>
/// Creates a new instance of PropertyComparer.
/// </summary>
/// <param name="propertyName">The name of the property on type T
/// to perform the comparison on.</param>
public PropertyComparer(string propertyName)
{
//store a reference to the property info object for use during the comparison
_PropertyInfo = typeof(T).GetProperty(propertyName,
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
if (_PropertyInfo == null)
{
throw new ArgumentException(string.Format("{0}
is not a property of type {1}.", propertyName, typeof(T)));
}
}
#region IEqualityComparer<T> Members
public bool Equals(T x, T y)
{
//get the current value of the comparison property of x and of y
object xValue = _PropertyInfo.GetValue(x, null);
object yValue = _PropertyInfo.GetValue(y, null);
//if the xValue is null then we consider them equal if and only if yValue is null
if (xValue == null)
return yValue == null;
//use the default comparer for whatever type the comparison property is.
return xValue.Equals(yValue);
}
public int GetHashCode(T obj)
{
//get the value of the comparison property out of obj
object propertyValue = _PropertyInfo.GetValue(obj, null);
if (propertyValue == null)
return 0;
else
return propertyValue.GetHashCode();
}
#endregion
}
你的表现会付诸东流。 当在集合数据结构上实现时, Distinct
和Except
是有效的操作。 通过提供恒定的哈希值,您基本上可以破坏此特征并使用线性搜索强制执行朴素算法。
您需要查看这对于您的数据量是否可以接受。 但是对于稍大的数据集,差异会很明显。 例如, Except
将预期时间为O(n)到为O(n²),它可以是一个大问题增加。
与其提供常量,不如直接调用对象自己的GetHashCode
方法? 它可能不会给出一个特别好的值,但它不会比使用常量更糟糕,并且正确性仍将保留,除非对象的GetHashCode
方法被覆盖以返回错误的值。
我需要将 Henrik 解决方案重写为一个实现IEqualityComparer
的类,它给出了这个:
public class GenericEqualityComparer<T,TKey> : IEqualityComparer<T>
{
private readonly Func<T, TKey> _keyFunction;
public GenericEqualityComparer(Func<T, TKey> keyFunction)
{
_keyFunction = keyFunction;
}
public bool Equals(T x, T y) => EqualityComparer<TKey>.Default.Equals(_keyFunction(x), _keyFunction(y));
public int GetHashCode(T obj)=> EqualityComparer<TKey>.Default.GetHashCode(_keyFunction(obj));
}
试试这个代码:
public class GenericCompare<T> : IEqualityComparer<T> where T : class
{
private Func<T, object> _expr { get; set; }
public GenericCompare(Func<T, object> expr)
{
this._expr = expr;
}
public bool Equals(T x, T y)
{
var first = _expr.Invoke(x);
var sec = _expr.Invoke(y);
if (first != null && first.Equals(sec))
return true;
else
return false;
}
public int GetHashCode(T obj)
{
return obj.GetHashCode();
}
}
示例: collection = collection.Except(ExistedDataEles, new GenericCompare(x=>x.Id)).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.