Hashset allows you to duplicate?

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!

+4
source share
5 answers

you use 2 fields to treat 2 objects as β€œequal”, but you use more than two fields to build a hash code. your hashCode() method cannot be more specific than your equals() method. as a rule, your hashCode() method should not use any fields that your equals() method does not use (it can use a smaller number). if you use 2 equal objects, they must have the same hash code (the opposite is not required).

+7
source

You violate the consistency requirement between hashCode () and equals (). If two objects are equal according to equals (), they must also have the same hash. Since your equals takes into account only two fields, and hashCode counts more, this requirement is not met.

+3
source

This is a recurring question, see previous answer.

The behavior when a java.util.HashSet allows duplication occurs when the hash code of objects in java.util.HashSet can change.

This usually happens when the hash code of an object is created from mutable fields.

0
source

Your problem is that the implementation of hashCode() does not match equals() . Both methods should use the same attributes of your object.

Your implementation probably hashCode() different, even if equals() evaluates to true . In this case (different hashCode s) the objects are different for the HashMap .

Please correct your implementations to use the same attributes. Then the error should disappear.

0
source

From javadoc Object

 If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. 

Your hash codes are different for two objects that are equal according to the equals(Object) method, so the other HashSet code will make incorrect assumptions and return incorrect results.

Some codes are written so that they depend on other objects that perform "contracts." Your class does not comply with the Object contract, so work cannot be used in collections, because collections require that Object contracts not be interrupted.

0
source

All Articles