由于杰克逊反序列化位置的id为null,因此hashCode为不同的对象返回相同的值,而equals返回true。集合不能包含等于的元素。解决您的equals
/ hashpre问题,一切都会正常进行。
建议的新hashCode /等于:
public int hashCode() { final int prime = 31; int result = 1; result = (int) (prime * result + ((id == null) ? 0 : id.hashCode())); result = (int) (prime * result + ((price== null) ? 0 : price.hashCode())); return result;}public boolean equals(Object obj) { if (obj == this) return true; if (!(obj instanceof Position)) return false; Position equalCheck = (Position) obj; if ((id == null && equalCheck.id != null) || (id != null && equalCheck.id == null)) return false; if (id != null && !id.equals(equalCheck.id)) return false; if ((price== null && equalCheck.price != null) || (price != null && equalCheck.price == null)) return false; if (price!= null && !price.equals(equalCheck.idprice) return false; return true;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)