Open In App

How to Convert a Comma-Separated String into an ArrayList in Java ?

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, a String is a sequence of characters. They are created using the String class and once a string is created, they are immutable which means that their value cannot be changed.

In this article, we will be learning how to convert a comma-separated String into an ArrayList in Java.

Syntax of ArrayList:

ArrayList<datatype> name = new ArrayList<>();

Methods to Convert a Comma-Separated String into an ArrayList

There are various ways of converting comma-separated strings and converting into ArrayList. Two methods are mentioned below.

Program to Convert a Comma-Separated String into an ArrayList in Java

Method 1: Using split()

Using the split() we can convert a comma-separated string into ArrayList as the split() method breaks a given string around the based expression. This method returns us a string array.

Approach:

  • The code initializes a string s.
  • Then the code uses split() method on the string s and passes `,` separator in the method argument.
  • The split() returns an array.
  • Arrays.asList() is used to create a List from the array.
  • new ArrayList<>() constructs a new ArrayList with the elements from the list.
  • In the end, the code prints the ArrayList.

Below is the code of the split() method implementation:

Java




// Java program to Convert a Comma-Separated
// String into an ArrayList using split()
import java.util.*;
  
// Driver Class
class GFG {
      // Main Function
    public static void main(String[] args)
    {
        // intializing a string
        String s = "Hello,from,geeks,for,geeks";
        // split the string by comma
        String[] arr = s.split(",");
        // Convert the array into arraylist
        ArrayList<String> list
            = new ArrayList<>(Arrays.asList(arr));
  
        // printing the arraylist
        System.out.println("The arraylist is : " + list);
    }
}


Output

The arraylist is : [Hello, from, geeks, for, geeks]

Explanation of the Program:

  • In the above program, it takes a string with comma-separated values, splits it into substrings using the split() method.
  • And it converts these substrings into an ArrayList.
  • Then, it prints the resulting ArrayList.

Method 2: Using Java Streams

Java Streams provide an efficient way to process collections of data.

Approach:

  • The code initializes a string s.
  • The code split() the input string into an array of strings using the comma`,` as a delimiter. The split() returns an array of substring.
  • Arrays.stream() converts the array of substrings into a stream of strings.
  • .collect(Collectors.toCollection(ArrayList :: new)) this will collect the stream of strings into a new ArrayList,through the collector all the collected elements should be stored in an ArrayList.

Java




// Java Program to Convert a Comma-Separated
// String into an ArrayList Using Stream
import java.util.*;
import java.util.stream.Collectors;
  
// Driver Class
class GFG {
      // Main Function
    public static void main(String[] args) {
        // Input comma-separated string
        String s = "Hello,from,geeks,for,geeks";
          
        // Split the string by comma and collect into ArrayList
        ArrayList<String> list = Arrays.stream(s.split(",")).collect(Collectors.toCollection(ArrayList::new));
          
        // Print the resultant ArrayList
        System.out.println("The resultant ArrayList is: " + list);
    }
}


Output

The resultant ArrayList is: [Hello, from, geeks, for, geeks]

Explanation of the Program:

  • In the above program, it takes a comma-separated string and converts it into an ArrayList.
  • It uses Java Streams to split the string, creating a stream of substrings.
  • Then, it collects these substrings into an ArrayList.
  • Finally, it prints the resulting ArrayList containing each word from the input string.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads