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 ?

Paragoumba
  • 88
  • 1
  • 10
  • 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. – Paragoumba Jul 09 '17 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 – OneCricketeer Jul 09 '17 at 12:11
  • I need to store the ArrayList for later, so I use a database but it's good i found an answer. – Paragoumba Jul 09 '17 at 12:24

3 Answers3

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).
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
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"));
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • there are absolutely no guarantees in the spec that commas are separating the individual tokens also – Eugene Jul 09 '17 at 14:04
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.

Emdadul Sawon
  • 5,730
  • 3
  • 45
  • 48