UN (hashcode: 2) _ DEUX (hashcode: 4)
map size: 2
UN (hashcode: 2) _ UN (hashcode: 2)
map size: 2
Dolphin 1
Both meaningfully equal
Roy Miro wrote:
both dolphins are in the same "bucket"
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
pete stein wrote:You've got a problem with your equals method:
You'll want to compare the name strings via the equals method not via the == operator.
Sven Mathijssen wrote:
Athough I agree with you in always comparing through the equals method, in this case it shouldn't matter because two Strings are compared from the String constant pool, returning true for the compared Strings, right?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sven Mathijssen wrote:
Athough I agree with you in always comparing through the equals method, in this case it shouldn't matter because two Strings are compared from the String constant pool, returning true for the compared Strings, right?
Abimaran Kugathasan wrote:
Sven Mathijssen wrote:
Athough I agree with you in always comparing through the equals method, in this case it shouldn't matter because two Strings are compared from the String constant pool, returning true for the compared Strings, right?
Exactly, I think, the story is different. There are two hashcode buckets, and those objects are in different buckets. So, even with the == opetator, the HashMap size 2.
If an object's hashCode() value can change based on its state, then we must be careful when using such objects as keys in hash-based collections to ensure that we don't allow their state to change when they are being used as hash keys. All hash-based collections assume that an object's hash value does not change while it is in use as a key in the collection. If a key's hash code were to change while it was in a collection, some unpredictable and confusing consequences could follow. This is usually not a problem in practice -- it is not common practice to use a mutable object like a List as a key in a HashMap.
pete stein wrote:
Sven Mathijssen wrote:
Athough I agree with you in always comparing through the equals method, in this case it shouldn't matter because two Strings are compared from the String constant pool, returning true for the compared Strings, right?
The key to the problem here though is that the OP's code corrupts the HashMap as keys are not supposed to be changed once used.
Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
SCJP6.0,My blog Ranchers from Delhi
Roy Miro wrote:After reading your replies, i conclude that:
1. Once a object is put in a bucket it cannot change for an other bucket even if the object's hashCode change (that's why the map size remains 2 in my example).
2. According to Sven Mathijssen links, although technically possible, a key object isn't due to have its state change. Otherwise, the map behavior isn't predictable.
Please confirm that my comprehension is correct.
Thanks a lot for your answers and impressive reactivity.![]()
Cheers,
Divyesh.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |