이 질문은 분명히 새로운 질문이 아니지만 어디에서나 유용한 답변을 찾지 못했습니다.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의에 항목에 추가하는 데 사용되는 코드 :이 코드 기능 (A)에 사용되는
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);
(allTasks가 HashSet의 인) 및 B.
을 함수 A 만 실행하면 정상적으로 작동합니다. 함수 B가 함수 A 다음에 실행되면 (위의 코드가 두 번 실행되므로) 중복 항목이 있다고 말하는 system.err가 발생하더라도 해시 세트는 갑자기 중복을 허용합니까?
코드에 결함이 있습니까? 아니면 방금 누락 되었습니까?
도움 주셔서 감사합니다.
나는 hashvalue가 변경되어 newTaskDetails를 조작하는 것으로 의심됩니다. – kosa
당신은 그 클래스를 대체하고 있습니까? –
"중복 허용"이란 정확하게 무엇을 의미합니까? 전체 객체가 같은가, 아니면 중복 된 ID를 의미 하는가? – Zutty