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 Parth
• 4,630 points
2,759 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,370 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, 2019 in Java by Sirajul
• 59,230 points
931 views
0 votes
2 answers

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

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

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
733 views
0 votes
1 answer

Convert ArrayList<String> to String[] array

Use like this. List<String> stockList = new ArrayList<String>(); stockList.add("stock1"); stockList.add("stock2"); String[] ...READ MORE

answered Dec 22, 2020 in Java by Gitika
• 65,910 points
926 views
+1 vote
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,290 points
963 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,910 points
1,236 views
0 votes
1 answer

Sorting an ArrayList in Java

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

answered May 5, 2018 in Java by geek.erkami
• 2,680 points
1,922 views
0 votes
1 answer

How do I join 2 lists

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

answered May 11, 2018 in Java by sharth
• 3,370 points
507 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, 2019 in Java by Sirajul
• 59,230 points
1,888 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,370 points
694 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP