i've following following vertex class , implements equals, hashcode , compareto method. hashmap returns null. don't know why?
public class vertex implements comparable<vertex> { int id; public vertex(int number) { id = number; } public boolean equals(object other) { if (other == null) return false; else if (other.getclass() != this.getclass()) return false; else { vertex copy = (vertex) other; if (copy.id == this.id) return true; else return false; } } public int hascode() { int prime = 31; int smallprime = 3; int hashcode = this.id ^ smallprime - prime * this.hascode(); return hashcode; } public int compareto(vertex other) { if (this.id < other.id) return -1; else if (this.id > other.id) return 1; else return 0; } }
your method called hascode()
. make hashcode()
instead.
i'd suggest using ide automatically generate hashcode()
, equals(..)
. generate proper methods (right have recursive call in hashcode()
)
Comments
Post a Comment