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

Java 8Object Oriented ProgrammingProgramming

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]
raja
Published on 17-Jan-2018 10:52:31
Advertisements