You need to override equals
@Override public boolean equals(Object obj) { if (!(obj instanceof Account)) return false; Account that = (Account) obj; return (account == null ? that.account == null : account .equals(that.account)) && balance == that.balance; }
I almost forgot to override hashCode when overriding equals
@Override public int hashCode() { int hash = 17; hash = 37 * hash + (account == null ? 0 : account.hashCode()); long l = Double.doubleToLongBits(balance); hash = 37 * hash + (int) (l ^ (l >>> 32)); return hash; }
source share