Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting back from toString to Object

Tags:

java

Is there any way to convert from toString back to the object in Java?

For example:

Map<String, String> myMap = new HashMap<String, String>();
myMap.put("value1", "test1");
myMap.put("value2", "test2");
String str = myMap.toString();

Is there any way to convert this String back to the Map?

like image 772
Tobias M Avatar asked Apr 07 '10 21:04

Tobias M


People also ask

How do you reverse toString?

To reverse the characters of a String object, we first need to convert it to a mutable StringBuilder object. Next, we need to call the reverse() method to reverse the character sequence of the string. Finally, we can obtain the reversed string from the StringBuilder object by calling the toString() method on it.

What is the return value of toString method of an object?

ToString() method. The default implementation of the ToString method returns the fully qualified name of the type of the Object, as the following example shows. Because Object is the base class of all reference types in the .


2 Answers

Short answer: no.

Slightly longer answer: not using toString. If the object in question supports serialization then you can go from the serialized string back to the in-memory object, but that's a whole 'nother ball of wax. Learn about serialization and deserialization to find out how to do this.

like image 190
JSBձոգչ Avatar answered Oct 16 '22 21:10

JSBձոգչ


No there isn't.

toString() is only intended for logging and debug purposes. It is not intended for serialising the stat of an Object.

like image 34
objects Avatar answered Oct 16 '22 22:10

objects