使用Java将HashMap.toString()转换回HashMap [英] Convert HashMap.toString() back to HashMap in Java

查看:1794
本文介绍了使用Java将HashMap.toString()转换回HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个键值对放入Java HashMap 中,并使用<$ $将其转换为 String c $ c $> toString()方法。

是否可以转换这个 String 表示回到 HashMap 对象,并用相应的键检索值?



谢谢

解决方案

使用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);

这篇关于使用Java将HashMap.toString()转换回HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆