Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap Constantly Overwriting

 
Carlos Duque
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm having problems with a basic HashMap, I don't know why the contents and key of "responseMessage", gets overwritten by "dto".

What's wrong with this code?

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing wrong. Why do you think it's being overwritten ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks ok so far. What happens next that isn't what you expect?
 
Carlos Duque
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I debug the code (Using Webshpere v5.1.2),

I see the "map" variable has a 'table' entry that works like an array, inside that map. here's the copy from the "variables" view:



Position [4] is where the responseMessage was before "dto" got there, but then I saw that responseMessage was stored inside the "next" Entry:



Why is that? I expected both of them to be side, by side, not one chained to the other...

Regards and thanks for your replies...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carlos Duque
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot !!!

Based on your reply I changed the key "dto" with "DTO" (uppercase), and now it's working. I'm still surprised that something this small cost me around 24 hours of reading and testing.


 
Paul Clapham
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carlos Duque
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic