5

I am using the toString method of ArrayList to store ArrayList data into a String. My question is, how do I go the other way? Is there an existing method that will parse the data in the String instance back into an ArrayList?

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
user174772
  • 163
  • 2
  • 2
  • 5
  • 4
    Note that if the elements of the List are not String objects then the reverse action might be impossible to do because `theList.toString()` will call `toString()` on all elements and that might or might not be a reversable action (i.e. the String representation might not contain all information necessary to reproduce the original object). – Joachim Sauer Oct 05 '09 at 07:01

6 Answers6

8

The short answer is "No". There is no simple way to re-import an Object from a String, since certain type information is lost in the toString() serialization.

However, for specific formats, and specific (known) types, you should be able to write code to parse a String manually:

// Takes Strings like "[a, b, c]"
public List parse(String s) {
  List output = new ArrayList();
  String listString = s.substring(1, s.length() - 1); // chop off brackets
  for (String token : new StringTokenizer(listString, ",")) {
    output.add(token.trim());
  }
  return output;
}

Reconstituting objects from their serialized form is generally called deserialization

Joel Raju
  • 1,210
  • 2
  • 18
  • 21
levik
  • 114,835
  • 27
  • 73
  • 90
3

Here's a similar question:

Reverse (parse the output) of Arrays.toString(int[])

It depends on what you're storing in the ArrayList, and whether or not those objects are easily reconstructed from their String representations.

Community
  • 1
  • 1
rledley
  • 2,325
  • 1
  • 12
  • 7
0

Apache Commons ftw.

Arrays.asList(StringUtils.split(StringUtils.substringBetween("[1, 2, 3]", "[", "]"), ", "))
Pyrolistical
  • 27,624
  • 21
  • 81
  • 106
0

I would recommend using some standard format with a library for it instead.

JSON is probably the closest syntactically.

Alternatively some XML or serialization based solution could work too. It all depends on your needs of course.

iny
  • 7,339
  • 3
  • 31
  • 36
  • The List.toString() format is pretty different from JSON. In fact it's a lot simple (and a lot less powerful). Using a JSON library is overkill (and probably just wrong). – Joachim Sauer Oct 05 '09 at 07:08
  • 1
    Implementing own buggy parser is just wrong. Using tested library is much better, always. – iny Oct 05 '09 at 17:24
0

What does the ArrayList consist of? As others said, it may be impossible in certain cases, but quite possible and guaranteed to work if:

  • each string representation of element of the array can be identified unambiguously (as it can be for example if the ArrayList consists of Integers)

  • there is a way to create an object of original type from its String representation. I find it most elegant to do this using a static method fromString

Of course, this mimics the whole (de)serialization framework.

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
0
private List<String> parseStringToList(String column) {
    List<String> output = new ArrayList<>();
    String listString = column.substring(1, column.length() - 1);
    StringTokenizer stringTokenizer = new StringTokenizer(listString,",");
    while (stringTokenizer.hasMoreTokens()){
      output.add(stringTokenizer.nextToken());
    }
    return output;
  }

In case the above gives a compilation error. (Can't use forEach on StringTokenizer)