Convert comma-separated String to ArrayList?

0 votes

Is there any inbuilt method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for that?

String commaSeparatedString = "item1 , item2 , item3";
method that converts above string into list?

May 17, 2018 in Java by parth295
• 4,620 points
323 views

1 answer to this question.

0 votes
List<String> alist = Arrays.asList(str.split("\\s*,\\s*"));

The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or more whitespace which will place the words into the list and collapse any whitespace between the words and commas.

answered May 17, 2018 by sharth
• 3,320 points

Related Questions In Java

0 votes
2 answers
0 votes
2 answers

what is the best way to convert an ArrayList to a String

You could probably use the Joiner class ...READ MORE

answered Aug 19 in Java by Sirajul
• 25,700 points
59 views
0 votes
2 answers

How to convert ArrayList<String> to String[] in Java

Try this:  String[] strArray = arrayList.toArray(new Stri ...READ MORE

answered Aug 21 in Java by Sirajul
• 25,700 points
68 views
0 votes
2 answers

How to convert an array to arraylist?

In Java 9 you can use: List<String> list= List.of("Hello", "World", ...READ MORE

answered Aug 1, 2018 in Java by samarth295
• 2,190 points
47 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,280 points
48 views
0 votes
2 answers

Remove duplicate elements from an ArrayList in Java

List<String> al = new ArrayList<>(); // add elements ...READ MORE

answered Jul 26, 2018 in Java by Sushmita
• 6,880 points
76 views
0 votes
1 answer

Sorting an ArrayList in Java

You can easily do this by simply ...READ MORE

answered May 4, 2018 in Java by geek.erkami
• 2,320 points
195 views
0 votes
1 answer

How do I join 2 lists

This is not a simpler method, but ...READ MORE

answered May 10, 2018 in Java by sharth
• 3,320 points
25 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 25,700 points
133 views
0 votes
1 answer

How to convert s Stack trace to a String?

One can use the given method to ...READ MORE

answered Jun 25, 2018 in Java by sharth
• 3,320 points
31 views