Ich habe das Problem, dass die Equals-Methode sagt, beide Instanzen sein "gleich", ich aber nicht den selben hashCode bekomme.
Die beiden Methoden sind von Eclipse auto-erzeugt. Die hashCode-Methode meiner Klasse ruft ja hashCode von einem Set auf.
Liegt der Fehler jetzt darin, dass ich die hashCode Methode jetzt wirklich selber einbauen muss und nicht die einer anderen Klasse nehmen darf (obwohl ich das equals der selben klasse nehme) oder doch wo anders?
in der Klasse
in der Testklasse
Die beiden Methoden sind von Eclipse auto-erzeugt. Die hashCode-Methode meiner Klasse ruft ja hashCode von einem Set auf.
Liegt der Fehler jetzt darin, dass ich die hashCode Methode jetzt wirklich selber einbauen muss und nicht die einer anderen Klasse nehmen darf (obwohl ich das equals der selben klasse nehme) oder doch wo anders?
in der Klasse
Java:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((points == null) ? 0 : points.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProxyTreeSet other = (ProxyTreeSet) obj;
if (points == null) {
if (other.points != null)
return false;
} else if (!points.equals(other.points))
return false;
return true;
}
in der Testklasse
Java:
@Test
public void testEquals()
{
System.out.println(">>> testEquals()");
points.add(new Point(0.1, 0.12));
points.add(new Point(0.2, 0.22));
points.add(new Point(0.3, 0.32));
ProxyTreeSetInterface tmpPoints = Factory.INSTANCE.createProxyTreeSetInterface();
tmpPoints.add(new Point(0.1, 0.12));
tmpPoints.add(new Point(0.2, 0.22));
tmpPoints.add(new Point(0.3, 0.32));
assertTrue(points.equals(tmpPoints));
System.out.println("\tpoints and tmpPoints are equal");
System.out.println("\ttmpPoints:");
System.out.println(getPointsStringRepresentation(points));
System.out.println("\tpoints:");
System.out.println(getPointsStringRepresentation(points));
}
@Test
public void testHashCode()
{
System.out.println(">>> testHashCode()");
points.add(new Point(0.1, 0.12));
points.add(new Point(0.2, 0.22));
points.add(new Point(0.3, 0.32));
ProxyTreeSetInterface tmpPoints = Factory.INSTANCE.createProxyTreeSetInterface();
tmpPoints.add(new Point(0.1, 0.12));
tmpPoints.add(new Point(0.2, 0.22));
tmpPoints.add(new Point(0.3, 0.32));
assertEquals(tmpPoints.hashCode(), points.hashCode());
System.out.println("\tpoints and tmpPoints have the same hashCode");
System.out.println("\tpoints hashCode: " + points.hashCode());
System.out.println("\ttmpPoints hashCode: " + tmpPoints.hashCode());
}