I have a String
object like
final String demoString = "1,2,19,12";
Now I want to create a Collection<String>
from it. How can I do that?
Guava:
List<String> it = Splitter.on(',').splitToList(demoString);
Standard JDK:
List<String> list = Arrays.asList(demoString.split(","))
Commons / Lang:
List<String> list = Arrays.asList(StringUtils.split(demoString, ","));
Note that you can't add or remove Elements from a List created by Arrays.asList, since the List is backed by the supplied array and arrays can't be resized. If you need to add or remove elements, you need to do this:
// This applies to all examples above
List<String> list = new ArrayList<String>(Arrays.asList( /*etc */ ))
Simple and good,
List<String> list = Arrays.asList(string.split(","))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With