Click to See Complete Forum and Search --> : visting all the elements of HashMap
zulqernain
June 6th, 2005, 05:26 PM
i have a hashMap in which i have stored the string while reading the file and now i want to go through that HashMap to get the values of all the strings and use it for calculation but the problem is that i dont know what the keys are other wise i would have used get method. there is no iterator (like vector) that can be used in it .i tried using keySet() but its not working .can you please tell me how i can get all the keys of the HashMap so that do the processing on the value.note that value of the keys in hashmap is Arraylist.
thanks a lot
wildfrog
June 6th, 2005, 06:40 PM
I would use keySet() and do something like this:
HashMap pMap = new HashMap();
// add arraylists to the hashmap
pMap.put("a", new ArrayList());
pMap.put("b", new ArrayList());
pMap.put("c", new ArrayList());
// Iterate through the hashmap
Iterator pIter = pMap.keySet().iterator();
while(pIter.hasNext())
{
String strKey = (String) pIter.next();
ArrayList pList = (ArrayList) pMap.get(strKey);
System.out.println(pList.size());
}
- petter
cjard
June 7th, 2005, 04:40 AM
hmm.. looks like someone isnt reading the documentation far down enough:
Collection values()
Returns a collection view of the values contained in this map.
zulqernain
June 7th, 2005, 05:45 PM
thanks a lot wildfrog.
cjard i read the documtatiion and i was trying to store the keys return ketSet in ArrayList but i was getting error.i did not think that i can use the itereator like this. thanks for the advice
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.