javacodex.com
Java Examples
Java Examples
memu home questions

Populate List With Comma Seperated Sting

This java example shows how populate a list from a string with a comma separated names.

Source: (PopulateList.java)

import java.util.*;
 
public class PopulateList {
   public static void main(String[] args) {
      String str = "qqq,www,eee,rrr,ttt,yyy";
      List<String> list = new ArrayList<>();
 
      System.out.println("String: " + str);
 
      for (String item: str.split("\\s*,\\s*")) {
         list.add(item);
      }
 
      System.out.println("List: " + list);
 
   }
}
 

Output:

$ java PopulateList 
String: qqq,www,eee,rrr,ttt,yyy
List: [qqq, www, eee, rrr, ttt, yyy]

Contact: javacodex@yahoo.com