你需要提供自己的
equals()in实现
MyClass。
@Overridepublic boolean equals(Object other) { if (!(other instanceof MyClass)) { return false; } MyClass that = (MyClass) other; // Custom equality check here. return this.field1.equals(that.field1) && this.field2.equals(that.field2);}hashCode()如果哈希表中有可能使用你的对象,则还应该重写。一个合理的实施将是该对象的字段的哈希码喜欢的东西结合起来:
@Overridepublic int hashCode() { int hashCode = 1; hashCode = hashCode * 37 + this.field1.hashCode(); hashCode = hashCode * 37 + this.field2.hashCode(); return hashCode;}


