我关注了以下 Vertex 类,它实现了 equals、hashCode 和 compareTo 方法。即使这样,我的 HashMap 也返回 null。我不知道为什么?
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;
}
}