Amandeep Singh Amandeep Singh - 5 months ago 8
Java Question

Converting ArrayList to String and converted String back to ArrayList

I am in a situation where I need to send the arraylist in the network but I need to convert it in the form of string.

Now at the destination I want to convert this String to ArrayList to access the individual elements.

Please tell me how I can convert the converted String(from ArrayList) back to ArrayList.

Answer

ArrayList of what? If the elements are strings or integers - simply join them using some unique separator and split back to array on the other side.

If you want to send an ArrayList of arbitrary Serializable objects, you will have to serialize it, e.g. using Java serialization. However this will produce byte[] array rather than a String. If you are constrained to text protocol, you would then gonna have to use Base64 encoding or similar.