Here's an example of going from a list of strings, to a single string, back to a list of strings.
Compiling:
$ javac test.java
$ java test
Running:
Initial list: "abc" "def" "ghi" "jkl" "mno" As single string: "[abc, def, ghi, jkl, mno]" Reconstituted list: "abc" "def" "ghi" "jkl" "mno"
Source code:
import java.util.*;
public class test {
public static void main(String[] args) {
List<String> listOfStrings= new ArrayList<>();
listOfStrings.add("abc");
listOfStrings.add("def");
listOfStrings.add("ghi");
listOfStrings.add("jkl");
listOfStrings.add("mno");
show("\nInitial list:", listOfStrings);
String singleString = listOfStrings.toString();
show("As single string:", singleString);
List<String> reconstitutedList = Arrays.asList(
singleString.substring(0, singleString.length() - 1)
.substring(1).split("[\\s,]+"));
show("Reconstituted list:", reconstitutedList);
}
public static void show(String title, Object operand) {
System.out.println(title + "\n");
if (operand instanceof String) {
System.out.println(" \"" + operand + "\"");
} else {
for (String string : (List<String>)operand)
System.out.println(" \"" + string + "\"");
}
System.out.println("\n");
}
}