How to convert a comma separated String into an ArrayList in Java?


To convert a comma separated String into an ArrayList

  1. Split the String into an array of Strings using the split() method.
  2. Now, convert the obtained String array to list using the asList() method of the Arrays class.

Example

 Live Demo

import java.util.Arrays;
import java.util.List;

public class Sample {
   public static void main(String[] args) {
      String myString = "JavaFx,Java,WebGL,OpenCV";
      String[] myArray = myString.split(",");
      System.out.println("Contents of the array ::"+Arrays.toString(myArray));
      List <String> myList = Arrays.asList(myArray);
   }
}

Output

Contents of the array ::[JavaFx, Java, WebGL, OpenCV]

Updated on: 30-Jul-2019

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements