Retrieving from a List which contains data as HashMap
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, August 27, 2003 11:16 PM

I am getting results from database and putting results in key value pair in HashMap as data. Then I add this data as HashMap in a list.

			

while (rs.next()) {
hmap.put(key,value);
.............
aList.add(hmap);
}


But when I retrieve:
			
Iterator it = accounts.iterator();
while (it.hasNext()) {
hmap= (Map) it.next();
System.out.println("hmap :" + hmap);
}

I always get the last record? Can anybody let me know where I am doing mistake?Please correct the snippet.

Re: Retrieving from a List which contains data as HashMap

Posted By:   arnold_espiritu  
Posted On:   Monday, September 1, 2003 04:14 AM

Instantiate the HashMap inside the while loop so that what you add in the list is always a new HashMap object,or else you will really get the latest key value pair...Hope that helps..




while (rs.next()) {

HashMap hmap = new HashMap();

hmap.put(key,value);

.............

aList.add(hmap);

}

Re: Retrieving from a List which contains data as HashMap

Posted By:   Anand_Kumar  
Posted On:   Thursday, August 28, 2003 03:33 AM

Your logic
while (rs.next()) {
hmap.put(key,value);
.............
aList.add(hmap);
}

is wrong.. Try to look one more time(!!)
About | Sitemap | Contact