Delete comment from: Javarevisited
vijay said...
System.out.println(set.size()); // Result is 1
System.out.println(set1.size()); // Result is 2
As String is immutable and use string pool concept, so hashmap handles differently in case of String for generating hashcode.
final int hash(Object k) {
int h = hashSeed;
if (0 != h && k instanceof String) {
return sun.misc.Hashing.stringHash32((String) k);
}
h ^= k.hashCode();
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
I hope you are clear.
Jan 24, 2016, 10:54:24 AM