1

I know this can easily be done by writing a function, however, I was wondering if there was a quick and convenient way to load a List in Java from its String representation.

I will give a small example:

List<String> atts = new LinkedList<String>();
atts.add("one"); atts.add("two"); atts.add("three”);
String inString = atts.toString()); //E.g. store string representation to DB
...
//Then we can re-create the list from its string representation?
LinkedLisst<String> atts2 = new LinkedList<String>();

Thnx!

Larry
  • 11,439
  • 15
  • 61
  • 84
  • possible duplicate of [What is the reverse of (ArrayList).toString for a Java ArrayList?](http://stackoverflow.com/questions/1518528/what-is-the-reverse-of-arraylist-tostring-for-a-java-arraylist) – Mark Elliot Jul 11 '11 at 14:27
  • If your objects are serializable (String is but I assume that's just an example) you could just deserialize the list. – Vlad Jul 11 '11 at 14:32

4 Answers4

2

You wouldn't want to use toString() for this. Instead you want to use java's serialize methods. Here is an example:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(stream);
out.writeObject(list);
stream.close();
// save stream.toByteArray() to db

// read byte
ByteArrayInputStream bytes = ...
ObjectInputStream in = new ObjectInputStream(bytes);
List<String> list = in.readObject();

This is a better solution because you don't have to split or parse anything. You can also use other methods of serilaizing such as json or xml. What I have showed above is built in Java.

Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
  • thanks, but could you please briefly explain the above code. For example where does the “stream” variable come from used in Line 2 and 4 of your code shown? – Larry Jul 11 '11 at 14:52
  • Larry, the stream var is from line 1. Part 1 is for writing and part 2 is for reading. For more info look here http://java.sun.com/developer/technicalArticles/Programming/serialization/ – Amir Raminfar Jul 11 '11 at 15:44
  • The problem is when I call stream.getBytes(), it returns an error saying that the method is not defined for this type?! – Larry Jul 11 '11 at 15:56
  • Sorry, it is actually called toByteArray(). Here is the java doc http://download.oracle.com/javase/6/docs/api/java/io/ByteArrayOutputStream.html – Amir Raminfar Jul 11 '11 at 16:50
1

//Then we can re-create the list from its string representation?

One option would be to agree to use a known format for both converting to and from the string representation. I would go with CSV here for that's simpler unless you have commas in your original string itself.

String csvString = "one,two,three";
List<String> listOfStrings = Arrays.asList(csvString.split(","));
adarshr
  • 61,315
  • 23
  • 138
  • 167
1

There is not a reliable way to do this. The toString() method for Lists is not designed to output something that can be reliably used as a way to serialise the list.

Here's way it can't work. look at this small change to your example:

public static void main(String[] args) {
    List<String> atts = new LinkedList<String>();
    atts.add("one");
    atts.add("two");
    atts.add("three, four");
    String inString = atts.toString();
    System.out.println("inString = " + inString);
}

This outputs

inString = [one, two, three, four]

This looks like the list contains four elements. But we only added three. There is not feasible way to determine the original source list.

Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
-1

You can create a List from its String representation. It may not be exactly the same List if the Strings contain a ", ", or a String is null but you can do it and it will work for many usecases.

List<String> strings = Arrays.asList("one", "two", "three");
String text = strins.asList();
// in this case, you will get the same list.
List<String> strings2=Arrays.asList(text.substring(1,text.length()-1).split(", "));
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130