tags:

views:

155

answers:

6

Hi

i am using HashMap in java. i put some key-value pair in the HashMap object and then i converted HashMap into an String using toString() method and pass to the another method that take an String and convert this String into HashMap object and retrieve the value with it's corresponding key.

Is it is possible to get the HashMap Object,please give me clear idea about.

Thanks

+2  A: 

What did you try?

objectOutputStream.writeObject(hashMap);

should work just fine, providing that all the objects in the hashMap implement Serializable.

djna
thanks for reply plz,could you explain this more detail.
sam
+3  A: 

Once you convert HashMap to String using toString(); It's not that you can concert back it to Hashmap from that String, Its just its String representation.

You can either pass the reference to HashMap to method or you can serialize it

Here is the description for toString() toString()
Here is the sample code with explanation for Serialization.

and to pass hashMap to method as arg.

public void sayHello(Map m){

}
//calling block  
Map  hm = new HashMap();
sayHello(hm);
org.life.java
Thanks for reply.
sam
plz,could you explain this more detail.
sam
@sam updated it
org.life.java
A: 

It is possible to rebuild a collection out of its string presentation but it will not work if the elements of the collection don't override their own toString method.

Therefore it's much safer and easier to use third party library like XStream which streams objects in human readable XML.

Boris Pavlović
+2  A: 

You cannot revert back from string to an Object. So you will need to do this:

HashMap<K, V> map = new HashMap<K, V>();

//Write:
OutputStream os = new FileOutputStream(fileName.ser);
ObjectOutput oo = new ObjectOutputStream(os);
oo.writeObject(map);
oo.close();

//Read:
InputStream is = new FileInputStream(fileName.ser);
ObjectInput oi = new ObjectInputStream(is);
HashMap<K, V> newMap = oi.readObject();
oi.close();
zengr
+2  A: 

i converted HashMap into an String using toString() method and pass to the another method that take an String and convert this String into HashMap object

This is a very, very bad way to pass around a HashMap.

It can theoretically work, but there's just way too much that can go wrong (and it will perform very badly). Obviously, in your case something does go wrong. We can't say what without seeing your code.

But a much better solution would be to change that "another method" so that it just takes a HashMap as parameter rather than a String representation of one.

Michael Borgwardt
A: 

It will work if toString() contains all data needed to restore the object. For example it will work for map of strings (where string is used as key and value):

// create map
Map<String, String> map = new HashMap<String, String>();
// populate the map

// create string representation
String str = map.toString();

// use properties to restore the map
Properties props = new Properties();
props.load(new StringReader(str.substring(1, str.length() - 1).replace(", ", "\n")));       
Map<String, String> map2 = new HashMap<String, String>();
for (Map.Entry<Object, Object> e : props.entrySet()) {
    map2.put((String)e.getKey(), (String)e.getValue());
}

This works although I really do not understand why do you need this.

AlexR