如果我们有一个像下面这样的类:
class ConnectedPair {
private String source;
private String destination;
public ConnectedPair(String source, String destination) {
if(source == null || destination == null) {
throw new NullPointerException();
}
this.source = source;
this.destination = destination;
}
}
我们想在 HashMap
或 HashSet
中使用它,我们需要实现 equals
和 hashCode
此类的自动生成的 hashCode
是:
@Override
public int hashCode() {
int result = source.hashCode();
result = 31 * result + destination.hashCode();
return result;
}
但我想知道我们是否做了以下事情:
@Override
public int hashCode() {
return (source + "=>" + destination).hashCode();
}
这比自动生成的方法好还是差?正如我所见,在其他脚本语言中也使用了类似的方法,我想知道生成的哈希是好是坏(或者没有区别)。
注意:假设它可以被缓存并因此被优化
答案 0 :(得分:2)
String.hashCode()
相当糟糕的定义的影响。由于历史原因,String.hashCode()
对它的工作方式有一个固定的定义,而这个定义是……糟糕的。它很容易发生意外碰撞,并且很容易构建故意碰撞。ConnectedPair
) 的 new ConnectedPair("a=>b", "c").hashCode() == new ConnectedPair("a","b=>c").hashCode()
对象非常变得简单。另一个选项并不能完全避免这种情况,但会使构建样本变得稍微困难一些。