This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Just bad luck. The HashMap computes an index into that array using the hashCode() of the key. It doesn't just use the hashCode() modulo the array size, but rather uses a more complex function of the hashCode(). Given 16 objects, it would be nice if every one hashed to a different "bucket" (a different index in the array) but chances are exceedingly high that there will be some "collisions" where two or more keys hash to the same value. That's what's happened here; the function returns the same value for your two keys. That's life! For a statistically significant number of objects, you'd see a nice even distribution. But if you think about it, given 16 buckets and two objects, the chances that they will end up in the same bucket is 1 in 16, or more than 6 per cent.
I think it's worth pointing out that there is no actual bug there. The fact that two keys hash into the same bucket is perfectly normal. So if you continue withyou will get back the same strings that you put in, with no surprises. So it looks like you spent the 24 hours for no useful purpose.
author and iconoclast
Originally posted by Paul Clapham: So it looks like you spent the 24 hours for no useful purpose.
Well, he learned something, anyway.
But indeed, don't spend time debugging API classes unless the results they give you seem to be wrong -- and don't spend time debugging any class unless you understand the algorithms it uses; tracing through a series of steps the purpose of which you don't understand is not a good use of time.
Joined: Sep 04, 2005
Just to be clear, I started debugging because I wasn't getting the responseMessage back with a simple:
But I did learn, now I know "it happens" and that I should start debugging classes until I *really* understand what they actually do.