java set 允许重复_java – Hashset允许重复?

这个问题肯定不是新问题,但我在任何地方都找不到任何有用的答案.

正如您在下面的代码中看到的那样,equals和hashcode方法被覆盖,但它仍然允许重复. Hashcode由Netbeans自动生成.

@Override

public boolean equals(Object o)

{

TaskDetails other = (TaskDetails) o;

if ( (id_subtask == other.id_subtask)

&& ((date.compareTo(other.date)) == 0) )

{

System.err.println("Duplicate Entry"+id_subtask+" + "+other.id_subtask);

return true;

}

else

{

System.out.println("Good!" +id_subtask+" + "+other.id_subtask);

return false;

}

}

@Override

public int hashCode() {

int hash = 7;

hash = 71 * hash + this.id_subtask;

hash = 71 * hash + this.id_team_member;

hash = 71 * hash + Float.floatToIntBits(this.nb_hours);

hash = 71 * hash + (this.date != null ? this.date.hashCode() : 0);

hash = 71 * hash + (this.comment != null ? this.comment.hashCode() : 0);

hash = 71 * hash + (this.subtask_name != null ? this.subtask_name.hashCode() : 0);

System.out.println("Hash : "+hash + "Subtask : " + id_subtask);

return hash;

}

这个代码用于在hashset中添加一个条目:

TaskDetails newTaskDetails = new TaskDetails

(

s.getId_subtask(),

mus.teamMember.getId_team_member(),

f,

mysqlFormat.format(caldate),

c.substring(0, Math.min(c.length(), 100)),

s.getName_subtask()

);

allTasks.add(newTaskDetails);

(allTask​​s是Hashset)

此代码用于函数A和B.

如果只执行功能A,它可以正常工作.如果在函数A之后执行函数B(所以上面的代码执行两次),那么即使触发了system.err说有重复的条目,hashset突然接受重复项?

代码中是否存在缺陷,或者我只是遗漏了什么?

谢谢您的帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值