aspose file tools*
The moose likes Beginning Java and the fly likes HashMap Constantly Overwriting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap Constantly Overwriting" Watch "HashMap Constantly Overwriting" New topic
Author

HashMap Constantly Overwriting

Carlos Duque
Greenhorn

Joined: Sep 04, 2005
Posts: 12
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Nothing wrong. Why do you think it's being overwritten ?


[My Blog]
All roads lead to JavaRanch
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This looks ok so far. What happens next that isn't what you expect?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Carlos Duque
Greenhorn

Joined: Sep 04, 2005
Posts: 12
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

Joined: Jul 08, 2003
Posts: 24185
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Carlos Duque
Greenhorn

Joined: Sep 04, 2005
Posts: 12
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jul 08, 2003
Posts: 24185
    
  34

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

Joined: Sep 04, 2005
Posts: 12
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 !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap Constantly Overwriting