File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes HashMap and the While Loop Story Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap and the While Loop Story" Watch "HashMap and the While Loop Story" New topic
Author

HashMap and the While Loop Story

robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
Hi

I have a while loop which iterates throught different users selected from database. I am here using two HashMaps(Hm1,Hm2). Hm1 iterates through each user enteries and store them. In the end of the loop i store the values collected in Hm2:

Hm2.put(id,Hm1);

where id is the unique id of each user from the database itself.

But strangely i am getting same values for Hm1 corresponding to each value of id. How do i store different values corresponding to each user??

Thanks & Regards
Robin


Life is short play hard.
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Robin,

Please Post Real Code. We can't help you figure out what's wrong with the loop without seeing it.


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
table contains id,name,date,calls

Say the database table contains records:
100 tom 20/04/2008 100
200 stevi 20/04/2008 200



Now what happens is when print the Hm2 i get the values of stevi corresponding to both id's 100 and 200 in the Hm2 HashMap.
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Robin,

Look at when and how often you're instantiating your Hm1 object.
[ April 28, 2008: Message edited by: Stevi Deter ]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

well robin,

Stevi is correct,

you instantiate array object in side the while loop...then you set value to the array from database...then you set the array to an hashmap
so every time new array object will create(it prevent the replacing existing value..)..

thanks & regards,
seetharaman
robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
Thanks for the response.

Actually I am instantiating the Hm1 and Hm2 only once outside the loop i am just putting the values in the while loop as shown in the code :


Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Robin,

So, you only create Hm1 once.

And on each iteration through while, you update the values and add it again to Hm2.

How do you think that explains the behavior you're seeing?
robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
Sorry Stevi
for the trouble I did the above based on the thought that the key name & calls of Hm1 once used would be put in Hm2 and then again the next entry of database would overwrite the previous values of name and calls and i would put them in Hm2. But got Hm2 contrary to what i thought.

But now when i am instantiating the Hm1 object inside the while loop and i get the correct Hm2.

Thanks & Regards
Robin
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

No need to apologize!

In my experience, it's having to work through these sorts of problems that really brings understanding of how everything actually works.
robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
OK! ya that's a noble thought.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap and the While Loop Story