重写hashCode() 问题,结果为什么是0而不是17?

2019-03-25 13:36|来源: 网路

public class LabelValue {
private String label;
private String value;

public LabelValue(final String label, final String value) {
this.label = label;
this.value = value;
}

public String getLabel() {
return this.label;
}

public void setLabel(String label) {
this.label = label;
}

public String getValue() {
return this.value;
}

public void setValue(String value) {
this.value = value;
}

public int hashCode() {
return (this.getValue() == null) ? 17 : this.getValue().hashCode();
}

public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof LabelValue)) {
return false;
}
LabelValue bean = (LabelValue) obj;
int nil = (this.getValue() == null) ? 1 : 0;
nil += (bean.getValue() == null) ? 1 : 0;
if (nil == 2) {
return true;
} else if (nil == 1) {
return false;
} else {
return this.getValue().equals(bean.getValue());
}

}
}


public class MyResult {
           public static void main(String[] args) {
LabelValue lv1 = new LabelValue("1","router");
LabelValue lv2 = new LabelValue("2","switch");
LabelValue lv3 = new LabelValue("3","router");
LabelValue lv4 = new LabelValue("4","");
System.out.println(lv4.hashCode());
}

}

相关问答

更多
  • 当然, a1和a2具有相同的散列码,但它们并不相同,因为您没有覆盖equals将两个具有相同ele A对象视为相等。 在使用散列码之后,地图将使用equals最终统治者的等号。 该地图将两个对象放在同一个存储桶中,但由于它们不相等,它将保留两者。 重写equals ,使其返回true如果另一个对象是A并且它们都具有相同的ele 。 然后你会看到val2将被返回以get通话。 Sure, a1 and a2 have the same hash code, but they weren't considere ...
  • 不要这样做。 hashCode()的约定表示两个相等的对象必须具有相同的哈希码。 它不保证任何不相等的对象。 这意味着你可以有两个完全不同的对象,但碰巧有相同的散列码,因此打破了你的equals() 。 在字符串之间获取哈希码冲突并不困难。 考虑JDK 8 String.hashCode()实现的核心循环: for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } 如果h的初始值为0 , val[i]是给定字符串中第i个字符的数 ...
  • 答案是肯定的。 在Java中,您可以在集合中添加对象。 让我们假设你想找到一个名为A的对象,并将其添加到名为L的列表中。让我们说这是一个你用自己的类定义的对象,并且重写Object#equals()方法。 当循环访问列表L时,您正在测试这些对象中的任何一个是否与对象A相同。如果equals方法返回true,那么您已找到您的对象。 将对象添加到任何HashTable,HashMap或HashSet时,会使用散列码方法生成一个数字。 这个数字应尽可能唯一。 有可能相同类的对象在其实例字段中具有不同的值,但它们的 ...
  • 属性不会覆盖hashCode和equals 。 您可以通过转到他们的文档页面(例如StringProperty )并向下滚动到它所说的“从类java.lang.Object继承的方法”来验证它。 如果列出了hashCode和equals ,那么它们不会被覆盖。 因此,例如, name.get().hashCode()将是正确的。 name.hashCode()将返回Object定义的标识哈希码。 equals也可以这样说。 例如, name.equals(rhs.name)将从Object调用基本实现,它与 ...
  • equals()和hashCode()在某些集合(如HashSet和HashMap hashCode()中被联合使用,因此您必须确保如果使用这些集合,则根据合约覆盖hashCode 。 如果你根本没有重写hashCode ,那么你会遇到HashSet和HashMap问题。 特别地,两个“相等”的对象可以放在不同的散列桶中,即使它们应该相等。 如果你重写hashCode ,但做得不好,那么你会遇到性能问题。 您所有的HashSet和HashMap条目都将放入同一个存储桶中,您将失去O(1)性能,并且具有O(n ...
  • 相信我。 不要写你自己的散列/等于逻辑。 使用已经可用且经过测试的代码,如Apache EqualsBuilder和HashCodeBuilder 例如: public int hashCode() { // you pick a hard-coded, randomly chosen, non-zero, odd number // ideally different for each class return new HashCodeBuilder(17, 37). ...
  • 这样的hashCode会工作。 您还必须以一致的方式覆盖equals : @Override public boolean equals(Object other) { if (!(other instanceof Player)) return false; Player op = (Player) other; return id == op.id; } Such hashCode would work. You'll also have to override ...
  • equals的默认实现是检查标识(即使用==运算符)。 您的LinkedHashSet (或其他任何HashSet ,对于这种情况)将包含唯一对象,因此无法将两个对象相加。 但是,如果以完全相同的方式创建两个实例(例如,将相同的参数传递给构造函数),那么您的集合仍然包含它们,因为它们不equals 。 The default implementation of equals is to check identity (i.e., use the == operator). Your LinkedHashSe ...
  • 对象的equals / hashcode实现很好 - 如果你希望“参考身份”作为你的平等。 换句话说,对象总是比较自己,但与另一个对象不同。 但是,如果您希望两个不同的对象相等,则必须覆盖该方法以说明它们应该如何相等(然后重写散列码以与其保持一致)。 最简单的例子可能是String。 两个具有相同字符的不同字符串是相同的,并且它们对于它们是相等的: String x = new String(new char[]{'a', 'b', 'c'}); String y = new String(new char ...
  • 使用; 分开陈述而不是。 我不认为练习的目的只是简单地连接线条并用分号分隔。 您应该尝试实际最小化代码。 例如: output = open(to_file, 'w') output.write(indata) print "Alright, all done." output.close() 可以替换为: with open(to_file, 'w') as output: output.write(indata) Use ; to separate statements instead of , ...