开始这样使用的时候一直报java.lang.StackOverflowError
public boolean equals(Object o) { if(this == o) return true; if(o == null || getClass() != o.getClass()) return false; HoldingInfo that = (HoldingInfo)o; return Objects.equals(performanceId, that.performanceId) && Objects.equals(name, that.name) && Objects.equals(weight, that.weight); } public int hashCode(){ return Objects.hash(performanceId,name,weight); }
修改后就好了,上面这种写法当执行equal检查时,它将一次又一次的被它自己调用,直到堆栈溢出
public boolean equals(Object o) { if(this.is(o)) return true; if(o == null || getClass() != o.getClass()) return false; HoldingInfo that = (HoldingInfo)o; return Objects.equals(performanceId, that.performanceId) && Objects.equals(name, that.name) && Objects.equals(weight, that.weight); } public int hashCode(){ return Objects.hash(performanceId,name,weight); }