HashMap

Hey Guys........I have created an Object and stored in a HashMap now I am not sure how to I retrieve the Object back from the data....(Display the values of the HashMap).
[184 byte] By [saranatora] at [2007-10-3 2:17:49]
# 1

> Hey Guys........

>

> I have created an Object and stored in a HashMap now

> I am not sure how to I retrieve the Object back from

> the data....(Display the values of the HashMap).

Yesterday, tsith and MLRon pointed you in the right direction. Did you read the advice provided?

Just so you don't ask this question again:

import java.util.*;

public class TestHashMap

{

public TestHashMap()

{

HashMap hm = new HashMap();

hm.put("One", new Integer(1));

hm.put("Two", new Integer(2));

Iterator iter = hm.entrySet().iterator();

for (int i = 0; i < hm.size(); i++)

{

Map.Entry entry = (Map.Entry) iter.next();

Object key = entry.getKey();

Object value = entry.getValue();

System.out.println(key+":"+value);

}

}

public static void main(String[] args)

{ new TestHashMap(); }

}

You're welcome.

Message was edited by:

filestream

filestreama at 2007-7-14 19:16:40 > top of Java-index,Java Essentials,New To Java...
# 2

Hi,

I understood that you have created a HashMap and stored an object into the same with some key. Now if you want to retrive the stored object you need to know the key, otherwise try this.....

Iterator iter = map.keySet().iterator();

while(iter.hasNext())

{

Object obj = map.get(iter.next()); //if you know the type of the object cast to it's specific type...........

}

I hope, I have answered to your question.............

Regards,

Mike

mohan_ksamya at 2007-7-14 19:16:40 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks guys ........FILESTREAM I did follow the advice dude but I had done something wrong else where.......but now I got it working.......ny ways sorry for bothering you guys again but I do appreciate the help.
saranatora at 2007-7-14 19:16:40 > top of Java-index,Java Essentials,New To Java...
# 4
Sorry, I am in a pissy mood. If you want to get the value associated with a key, then hm.get(Object key);.But my tsith and MLRon comment still stands.
filestreama at 2007-7-14 19:16:40 > top of Java-index,Java Essentials,New To Java...