3

I have this code:

ArrayList<String> list = new ArrayList<>();
String str = list.toString(); // Stored in a database

Is there a quick way to recover the ArrayList from the string ?

3
  • Yes, I know the way that consists to split the string at each comma but i search if there's a faster way, like a simple method. Commented Jul 9, 2017 at 12:08
  • 1
    Please explain in more detail what you need. You already have an Arraylist object before you made a string of it Commented Jul 9, 2017 at 12:11
  • I need to store the ArrayList for later, so I use a database but it's good i found an answer. Commented Jul 9, 2017 at 12:24

3 Answers 3

3

No, there is no reliable way to do that.

Consider the following:

List<String> list = new ArrayList<>();
list.add("A");
list.add("B, C");  // this contains commas 
list.add("D");

System.out.println(list);  // [A, B, C, D] : Note, only three elements were added 

A couple of solutions:

  • Use a proper serialisation mechanism (e.g. JSON) rather than toString().
  • Store your data in a more normalised way (rather than serialising into to a single record).
Sign up to request clarification or add additional context in comments.

Comments

2

There is no one-size-fits-all solution because ArrayList.toString() method will print all the elements separated by comma. But there is absolutely no way to find out whether the comma in your outputted string is because of separation of elements or was it literally included in the string (or object).

Consider these two code snippets:

List<String> list = new ArrayList<>();
list.add("one");
list.add("two, three");
System.out.println(list);  // [one, two, three]

and

List<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list);  // [one, two, three]

As you can see, both of these examples are giving exactly the same output.


However, if the elements in your list are some other type, (let's say Integer, Double etc... or even String without a comma), Then you can simply get your list back using

 Arrays.asList(outString.split(",\\s"));

1 Comment

there are absolutely no guarantees in the spec that commas are separating the individual tokens also
0

Sure, you can do it.

 ArrayList<String> list = new ArrayList<>();
 String str = list.toString();

 int len = str.length();
 str = str.substring(1, len-1);
 String[] parts = str.split(",");
 List<String> list2 = Arrays.asList(parts);

But this has one limitation. If any value of list contaion comma(,) then it will give you wrong output. In that case you should escape comma before convert it to String. Or you can use any serialization mechanism like JSON

Another approach is, do not call toString() directly. Instead you can use String Joiner (Example can be found here). Use any safe character. Then split your string to get the list back.

2 Comments

Only if the elements are already strings and don't contain intenal commas.
Yes, I know. But this answer should not get downvote. It is not a wrong answer, and it does not violet any rule.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.