- How to Convert String to ArrayList in Java
- Time for an Example:
- Example 1
- Example 2
- Convert String to ArrayList in Java
- Convert a String Into ArrayList Using the charAt() and add() Methods in Java
- Convert a String Into ArrayList Using the toCharArray() Method in Java
- the split() Method in Java
- Convert a String Into ArrayList Using the split() Method in Java
- Create an ArrayList Using the split() Method in Java
- Convert User String Into ArrayList in Java
- Convert a String Array Into ArrayList in Java
- Convert a String Into ArrayList in Java
- Related Article — Java String
- Related Article — Java ArrayList
- Related Article — Java Array
- Java – How to Convert a String to ArrayList
- Program to Convert String to ArrayList
- Top Related Articles:
- About the Author
How to Convert String to ArrayList in Java
In this post, we are going to convert a string to ArrayList using Java code. The string is a sequence of characters and a class in Java while the ArrayList is an implementation class of list interface.
Suppose, we have a URL string that consists of a server resource path and separated by some separate symbols, and we want to get it as ArrayList . So, we need to perform this conversion.
To convert string to ArrayList, we are using asList() , split() and add() methods. The asList() method belongs to the Arrays class and returns a list from an array.
The split() method belongs to the String class and returns an array based on the specified split delimiter.
Here, we have several examples to illustrate the string to ArrayList conversion process. See the examples below.
Time for an Example:
Let’s take an example to get an Arraylist from a string. Here, we are using the split() method to get an array of strings and then converting this array into a list using the asList() method.
import java.util.ArrayList; import java.util.Arrays; public class Main < public static void main(String[] args)< String msg = "StudyTonight.com/tutorial/java/string"; System.out.println(msg); // string to ArrayList ArrayListlist = new ArrayList<>(Arrays.asList(msg.split("/"))); list.forEach(System.out::println); > >
StudyTonight.com/tutorial/java/string
StudyTonight.com
tutorial
java
string
Example 1
If we have an array of strings then we don’t need to split() method. We can directly pass this array into asList() method to get ArrayList . See the example below.
import java.util.ArrayList; import java.util.Arrays; public class Main < public static void main(String[] args)< String[] msg = ; System.out.println(msg.length); // string[] to ArrayList ArrayList list = new ArrayList<>(Arrays.asList(msg)); list.forEach(System.out::println); > >
4
StudyTonight.com
tutorial
java
string
Example 2
Let’s take another example to get ArrayList, Here, we are using add() method to add string array elements to the ArrayList by traversing. This is easiest and simple for beginners.
import java.util.ArrayList; public class Main < public static void main(String[] args)< String[] msg = ; System.out.println(msg.length); // string[] to ArrayList ArrayList list = new ArrayList<>(); for (String string : msg) < list.add(string); >System.out.println(list); > >
4
[StudyTonight.com, tutorial, java, string]
Convert String to ArrayList in Java
- Convert a String Into ArrayList Using the charAt() and add() Methods in Java
- Convert a String Into ArrayList Using the toCharArray() Method in Java
- the split() Method in Java
- Convert a String Into ArrayList Using the split() Method in Java
- Create an ArrayList Using the split() Method in Java
- Convert User String Into ArrayList in Java
- Convert a String Array Into ArrayList in Java
- Convert a String Into ArrayList in Java
This tutorial introduces the conversion of String to ArrayList in Java and lists some example codes to understand the topic.
A string is defined as a sequence of characters, and an ArrayList is used to store an ordered sequence of data. Converting a string to an ArrayList requires us to take each character from the string and add it to the ArrayList .
In this article, we’ll discuss the different ways by which we can do this.
Convert a String Into ArrayList Using the charAt() and add() Methods in Java
A simple solution can be to iterate through each character of the string and add that character to the ArrayList . We will use the charAt() method to access the characters of the string, and then we can add them to the ArrayList by using the add() method.
The code for this approach is shown below.
import java.util.ArrayList; public class SimpleTesting public static void main(String[] args) String s = "sample"; ArrayListCharacter> list = new ArrayListCharacter>(); for(int i = 0; i s.length(); i++) char currentCharacter = s.charAt(i);//getting the character at current index list.add(currentCharacter);//adding the character to the list > System.out.println("The String is: " + s); System.out.print("The ArrayList is: " + list); > >
The String is: sample The ArrayList is: [s, a, m, p, l, e]
We cannot use this approach if we wish to do something more advanced. For example, if we want to add just the words of a sentence to an ArrayList and ignore the punctuations, further processing will be required.
Convert a String Into ArrayList Using the toCharArray() Method in Java
The toCharArray() method can be used on a string to convert it into a character array. We can then iterate over this character array and add each character to the ArrayList .
import java.util.ArrayList; public class Main public static void main(String[] args) String s = "sample"; ArrayListCharacter> list = new ArrayListCharacter>(); char[] characterArray = s.toCharArray(); for(char c : characterArray)//iterating through the character array list.add(c); System.out.println("The String is: " + s); System.out.print("The ArrayList is: " + list); > >
The String is: sample The ArrayList is: [s, a, m, p, l, e]
This is a simple method that can be used if we don’t want to do anything complicated. However, just like the approach discussed in the previous section, we cannot use this if we’re going to do some processing on the string (like removing punctuations) before converting it to an ArrayList .
the split() Method in Java
The string split() method takes a regular expression or pattern as a parameter and splits the string into an array of strings depending on the matching patterns. This method returns a string array.
For example, if we pass the string string of words to the split() method, and the pattern is single whitespace (denoted by //s+ ), then it will return the array [«string», «of», «words»] .
import java.util.Arrays; public class Main public static void main(String[] args) String s = "string of words"; String[] strArr = s.split("\\s+");//Splitting using whitespace System.out.println("The String is: " + s); System.out.print("The String Array after splitting is: " + Arrays.toString(strArr)); > >
The String is: string of words The String Array after splitting is: [string, of, words]
Convert a String Into ArrayList Using the split() Method in Java
We can create an ArrayList from the returned array of strings by using the asList() method of the Arrays class. The following code demonstrates this.
import java.util.ArrayList; import java.util.Arrays; public class Main public static void main(String[] args) String s = "string of words"; String[] strArr = s.split("\\s+");//Splitting using whitespace ArrayListString> list = new ArrayListString>(Arrays.asList(strArr)); System.out.println("The String is: " + s); System.out.print("The ArrayList is: " + list); > >
The String is: string of words The ArrayList is: [string, of, words]
Create an ArrayList Using the split() Method in Java
The split() method needs to be changed according to our needs. For example, if we want to create an ArrayList of individual characters of the string sample , then the split() method needs a different regular expression.
import java.util.ArrayList; import java.util.Arrays; public class Main public static void main(String[] args) String s = "sample"; String[] strArr = s.split(""); //Splitting string into individual characters ArrayListString> list = new ArrayListString>(Arrays.asList(strArr)); System.out.println("The String is: " + s); System.out.print("The ArrayList is: " + list); > >
The String is: sample The ArrayList is: [s, a, m, p, l, e]
Convert User String Into ArrayList in Java
Suppose we take an input string from the user that contains comma-separated employee names, and we have to make an ArrayList that includes each employee’s name.
We can use the split() method to split the string into an array of employee names, and then we can simply convert it to an ArrayList . The split() method is perfect for this task as we need to remove the commas before creating the ArrayList .
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main public static void main(String[] args) String employeeNames; Scanner scanner = new Scanner(System.in); System.out.println("Enter employee names separated by comma"); employeeNames = scanner.nextLine();//taking user input scanner.close(); String[] employeeNamesSplit = employeeNames.split(",");//Splitting names ArrayListString> list = new ArrayListString>(Arrays.asList(employeeNamesSplit)); System.out.println("The String is: " + employeeNames); System.out.print("The ArrayList is: " + list); > >
Enter employee names separated by comma Justin, Jack, Jessica, Victor The String is: Justin, Jack, Jessica, Victor The ArrayList is: [Justin, Jack, Jessica, Victor]
Convert a String Array Into ArrayList in Java
Arrays are great for storing data in an ordered fashion, but their limited size can restrict some important operations. We can simply convert a string array to an ArrayList by using the asList() method. It will simply copy all items of the array into the list.
The following code demonstrates this.
import java.util.ArrayList; import java.util.Arrays; public class Main public static void main(String[] args) String[] strArr = "Hello", "Hola", "Ola">; ArrayListString> strList = new ArrayListString>(Arrays.asList(strArr)); System.out.println("The String Array is: " + Arrays.toString(strArr)); System.out.println("The Array List is: " + strList); > >
The String Array is: [Hello, Hola, Ola] The Array List is: [Hello, Hola, Ola]
Convert a String Into ArrayList in Java
Strings are commonly used for many different purposes, but they are immutable, and we cannot apply changes to them. An ArrayList , on the other hand, provides a lot more flexibility. We can create an ArrayList from the individual characters of the string, or if we need to do something more complicated (like creating an ArrayList of names from a comma-separated string), we can use the split() method.
Overall, the split() method is the easiest and the most preferred way of converting a string to an ArrayList .
Related Article — Java String
Related Article — Java ArrayList
Related Article — Java Array
Java – How to Convert a String to ArrayList
In this java tutorial, you will learn how to convert a String to an ArrayList.
Input: 22,33,44,55,66,77
Delimiter: , (comma)
Output: ArrayList with 6 elements
Input: Welcome to BeginnersBook
Delimiter: ” ” (whitespace)
Output: ArrayList with 3 elements
The steps to convert string to ArrayList:
1) First split the string using String split() method and assign the substrings into an array of strings. We can split the string based on any character, expression etc.
2) Create an ArrayList and copy the element of string array to newly created ArrayList using Arrays.asList() method. This method returns a list created based on the elements of the specified array.
Program to Convert String to ArrayList
In this java program, we are converting a comma separated string to arraylist in java. The input string consists of numbers and commas. We are splitting the string based on comma (,) as delimiter to get substrings. These substrings are assigned to ArrayList as elements.
import java.util.ArrayList; import java.util.List; import java.util.Arrays; public class JavaExample < public static void main(String args[])< String num = "22,33,44,55,66,77"; String str[] = num.split(","); Listal = new ArrayList(); al = Arrays.asList(str); for(String s: al) < System.out.println(s); >> >
Note: In the above example, the delimiter is comma however we can split the string based on any delimiter. For example: if the string is “hello hi namaste bye” then we can split the string using whitespace as delimiter like this –
Here we have provided whitespace as delimiter String str[] = num.split(" ");
Top Related Articles:
About the Author
I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.