This question, of course, is not new, but I have not found a useful answer anywhere.
As you can see in the code below, the equals and hashcode methods are overridden, but still allow duplication. Hashcode is automatically generated by 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; }
This code is used to add entries to the 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);
(allTasks is a Hashset)
This code is used in functions A and B.
If only function A is executed, it works fine. If function B is executed after function A (therefore, the code above is executed twice), then the hashset suddenly accepts duplicates, even if system.err is started, saying that there is a duplicate entry?
Is there a flaw in the code, or am I just missing something?
Thanks for the help!
source share