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

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

问题描述

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

是否可以将此 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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