且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Java将HashMap.toString()转换回HashMap

更新时间:2023-09-26 21:09:10

使用toString()将HashMap转换为String后,这不是说你可以从它的String转换回Hashmap,它只是它的String表示。



您可以将引用传递给HashMap,也可以序列化它。



这里是toString() toString()

这里是带有序列化解释的示例代码。

并将hashMap传递给arg。

  public void sayHello(Map m){

}
// call block
Map hm = new HashMap();
sayHello(hm);


I put a key-value pair in a Java HashMap and converted it to a String using the toString() method.

Is it possible to convert this String representation back to a HashMap object and retrieve the value with its corresponding key?

Thanks

Once you convert HashMap to String using toString(); It's not that you can convert 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);