i added the list of arraylists to the haspmap.when i try to get it those from hashmap it is giving classcast exception.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   padmakar_kadem
Posted On:   Wednesday, September 12, 2007 08:50 AM

How to get the list of arrays from hashmap.the code is like this HashMap hm=new HashMap(); ArrayList arr = new ArrayList(); ArrayList arr1 = new ArrayList(); ArrayList arr2 = new ArrayList(); arr.add("usd"); arr.add("usd"); arr1.add("jpy"); arr1.add("jpy"); arr2.add("ind"); arr2.add("ind"); hm.put("usd","arr"); hm.put("jpy","arr1"); hm.put("ind","arr2"); Set st=hm.keySet(); Iterator it =st.iterator(); while(it.hasNext()) { String str = (String)(it.next()); if(hm.containsKey("usd")){    More>>

How to get the list of arrays from hashmap.the code is like this
HashMap hm=new HashMap();
ArrayList arr = new ArrayList();
ArrayList arr1 = new ArrayList();
ArrayList arr2 = new ArrayList();
arr.add("usd");
arr.add("usd");
arr1.add("jpy");
arr1.add("jpy");
arr2.add("ind");
arr2.add("ind");
hm.put("usd","arr");
hm.put("jpy","arr1");
hm.put("ind","arr2");
Set st=hm.keySet();
Iterator it =st.iterator();
while(it.hasNext())
{
String str = (String)(it.next());
if(hm.containsKey("usd")){
(problem is here)---->ArrayList array = (ArrayList) hm.getstr);
for(int i=0;i
{
System.out.println("a1 values" + " - " +array.get(i));
}

}

   <<Less

Re: i added the list of arraylists to the haspmap.when i try to get it those from hashmap it is giving classcast exception.

Posted By:   padmakar_kadem  
Posted On:   Thursday, September 13, 2007 05:55 AM

thank u for given the reply
There is a small mistake in the code .the code is like this

//list of arrays added in the hashmap

Set st=hm.keySet();
Iterator it =st.iterator();
while(it.hasNext())
{
String str = (String)(it.next());
if(hm.containsKey("usd")){
ArrayList array = (ArrayList) hm.get(str);
for(int i=0;i {
System.out.println("a1 values" + " - " + array.get(i));
}

}

i am getting the error(class cast exception) in this line: ArrayList array = (ArrayList) hm.get(str);

Re: i added the list of arraylists to the haspmap.when i try to get it those from hashmap it is giving classcast exception.

Posted By:   Robert_Lybarger  
Posted On:   Wednesday, September 12, 2007 09:14 AM

well the first problem is that "getstr)" is probably a typo; and second, HashMap does not contain a method called getstr() anyway. Third, looks like you are trying to store a String constant "arr" into the hashmap, then somehow magically you want it cast back out to an ArrayList. Is that about right?


How about you try storing the actual ArrayList object instead of a String constant by the same name:



ArrayList arr = new ArrayList();
HashMap hm = new HashMap();
hm.put("usd", arr); // attention!
...
ArrayList array = (ArrayList) hm.get("usd"); // attention!
...
About | Sitemap | Contact