
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert String to List of Characters in Java
In this article, we will learn how to convert a string to a list of characters in Java. Sometimes, when we handle character-based data structures, we need to convert a string into a list of characters.
In Java, a string is a sequence of characters and is represented by the String class. A list is a collection of elements and is represented by the List interface.
Converting a String to a List of Characters
The following are the different approaches to convert a string to a list of characters in Java -
- Using String.toCharArray()
- Using String.toList()
- Using StringBuilder
- Using String.valueOf()
- Using Java Streams
Using String.toCharArray()
The String.toCharArray() method converts a string into an array of characters. This method returns a new character array containing the same characters as the string.
Example
Following are the steps to convert a string to a list of characters using String.toCharArray() -
- The String.toCharArray() method is called on the string to convert it into a character array.
- The Arrays.asList() method is used to convert the character array into a list.
- The List interface is used to store the characters.
- Finally, the list is printed.
Let's say the following is our string -
String str = "Welcome";
Convert the string to a list of characters -
List<Character> list = Arrays.asList(str.toCharArray());
Here, the Arrays.asList() method is used to convert the character array into a list of characters.
Example
Below is an example of converting a string to a list of characters using String.toCharArray() -
import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) { String str = "Welcome"; Listlist = new ArrayList<>(); for (char ch : str.toCharArray()) { list.add(ch); // Autoboxing from char to Character } System.out.println("List of characters = " + list); } }
Output
List of characters = [W, e, l, c, o, m, e]
Time Complexity: O(N), each character is processed once. Space Complexity: O(N), creates a new character array and list object.
Using String.toList()
The String.toList() method converts a string into a list of characters. This method returns a new list containing the same characters as the string. Following are the steps to convert a string to a list of characters using String.toList() -
- The String.toList() method is called on the string to convert it into a list of characters.
- The List interface is used to store the characters.
- Finally, the list is printed.
Let's say the following is our string -
String str = "Welcome";
Convert the string to a list of characters -
List<Character> list = str.toList();
Here, the String.toList() method is used to convert the string into a list of characters.
Example
Below is an example of converting a string to a list of characters using String.toList() -
import java.util.List; import java.util.ArrayList; public class Demo { public static void main(String[] args) { String str = "Welcome"; List<Character> list = new ArrayList<>(); for (char c : str.toCharArray()) { list.add(c); } System.out.println("List of characters = "+list); } }
Output
List of characters = [W, e, l, c, o, m, e]
Time Complexity: O(N), each character is processed once. Space Complexity: O(N), creates a new character array and list object.
Using StringBuilder
The StringBuilder class offers a more efficient way to convert a string to a list of characters. This method is faster than the String.toCharArray() method. Following are the steps to convert a string to a list of characters using StringBuilder -
- A StringBuilder object is initialized.
- A for-each loop iterates over the string, appending each character.
- The List interface is used to store the characters.
- Finally, the list is printed.
A StringBuilder object sb, is created using the StringBuilder class -
StringBuilder sb = new StringBuilder();
Convert the StringBuilder object into a list of characters -
List<Character> list = new ArrayList<>();
Here, the ArrayList class is used to create a list of characters. Convert the StringBuilder object into a list of characters -
for (char c : sb.toString().toCharArray()) { list.add(c); }
Here, the ArrayList class is used to create a list of characters.
Example
Below is an example of converting a string to a list of characters using StringBuilder -
import java.util.List; import java.util.ArrayList; import java.lang.StringBuilder; public class Demo { public static void main(String[] args) { String str = "Welcome"; StringBuilder sb = new StringBuilder(str); List<Character> list = new ArrayList<>(); for (char c : sb.toString().toCharArray()) { list.add(c); } System.out.println("List of characters = "+list); } }
Output
List of characters = [W, e, l, c, o, m, e]
Time Complexity: O(N), each character is processed once. Space Complexity: O(N), creates a new character array and list object.
Using String.valueOf()
The String.valueOf() method converts a character to a string. This method returns a new string containing the same characters as the original string. Following are the steps to convert a string to a list of characters using String.valueOf() -
- The String.valueOf() method is called on the string to convert it into a character.
- The List interface is used to store the characters.
- Finally, the list is printed.
Example
Below is an example of converting a string to a list of characters using String.valueOf() -
import java.util.List; import java.util.ArrayList; import java.lang.String; public class Demo { public static void main(String[] args) { String str = "Welcome"; Listlist = new ArrayList<>(); for (char c : str.toCharArray()) { list.add(c); // Correct: adding the actual character } System.out.println("List of characters = "+list); } }
Output
List of characters = [W, e, l, c, o, m, e]
Time Complexity: O(N), each character is processed once. Space Complexity: O(N), creates a new character array and list object.
Using Java Streams
Java Streams provide a functional way to process collections. The map() function is used to convert each character into a string, Collectors.joining() is used to concatenate them. Following are the steps to convert a string to a list of characters using Java streams ?
- stream() to convert the string into a stream.
- map(String::valueOf) converts each character to a string.
- collect(Collectors.toList()) collects all string elements into a list.
- Finally, the list is printed.
Example
Below is an example of converting a string to a list of characters using Java streams -
import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import java.lang.String; public class Demo { public static void main(String[] args) { String str = "Welcome"; List<Character> list = str.chars().mapToObj(c -> (char)c).collect(Collectors.toList()); System.out.println("List of characters = "+list); } }
Output
List of characters = [W, e, l, c, o, m, e]
Time Complexity: O(N), each character is processed once. Space Complexity: O(N), creates a new character array and list object.