Convert HashMap.toString() back to HashMap in Java(在 Java 中将 HashMap.toString() 转换回 HashMap)
问题描述
我将一个键值对放入 Java HashMap 中,并使用
p>toString()
方法将其转换为 String
.
是否可以将此 String
表示形式转换回 HashMap
对象并使用其对应的键检索值?
谢谢
toString()
方法依赖于 toString()
的实现,在大多数情况下它可能是有损的案例.
这里不可能有无损解决方案.但更好的方法是使用对象序列化
将对象序列化为字符串
private static String serialize(Serializable o) throws IOException {ByteArrayOutputStream baos = new ByteArrayOutputStream();ObjectOutputStream oos = new ObjectOutputStream(baos);oos.writeObject(o);oos.close();返回 Base64.getEncoder().encodeToString(baos.toByteArray());}
将字符串反序列化回对象
private static Object deserialize(String s) throws IOException,类NotFoundException {byte[] data = Base64.getDecoder().decode(s);对象输入流 ois = 新对象输入流(新的字节数组输入流(数据));对象 o = ois.readObject();ois.close();返回 o;}
这里如果用户对象的字段是瞬态的,它们会在这个过程中丢失.
<小时>旧答案
<小时>一旦你使用 toString() 将 HashMap 转换为字符串;并不是说您可以将其从该字符串转换回 Hashmap,它只是它的字符串表示形式.
您可以将 HashMap 的引用传递给方法,也可以将其序列化
这里是 toString() 的描述 toString()
这里是带有序列化解释的示例代码.
并将 hashMap 作为 arg 传递给方法.
public void sayHello(Map m){}//调用块映射 hm = new HashMap();说你好(嗯);
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
toString()
approach relies on implementation of toString()
and it can be lossy in most of the cases.
There cannot be non lossy solution here. but a better one would be to use Object serialization
serialize Object to String
private static String serialize(Serializable o) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
deserialize String back to Object
private static Object deserialize(String s) throws IOException,
ClassNotFoundException {
byte[] data = Base64.getDecoder().decode(s);
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream(data));
Object o = ois.readObject();
ois.close();
return o;
}
Here if the user object has fields which are transient, they will be lost in the process.
old answer
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中将 HashMap.toString() 转换回 HashMap


基础教程推荐
- 验证是否调用了所有 getter 方法 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 大摇大摆的枚举 2022-01-01