Convert List of Characters to String in Java
Given a list of characters, convert the list to a string.
Input : list = {‘g’, ‘e’, ‘e’, ‘k’, ‘s’}
Output : “geeks”Input : list = {‘a’, ‘b’, ‘c’}
Output : “abc”
Convert List of Characters to String in Java:
1. Using StringBuilder class
Simple solution would be to iterate through the list and create a new string with the help of StringBuilder class as shown below:
// Java program for convert character list to string import java.util.Arrays; import java.util.List; // Convert List of Characters to String in Java class GFG { public static void main(String[] args) { // create character list and initialize List<Character> str = Arrays.asList('G', 'e', 'e', 'k', 's'); // create object of StringBuilder class StringBuilder sb = new StringBuilder(); // Appends characters one by one for (Character ch : str) { sb.append(ch); } // convert in string String string = sb.toString(); // print string System.out.println(string); } } |
Geeks
2. Using join() method of joiner class :
Joiner class can be used to join pieces to text specified as an array, and return the results as a string.This method also called Guava method.
// Java program for convert character list to string import com.google.common.base.Joiner; import java.util.Arrays; import java.util.List; // Convert List of Characters to String in Java class GFG { public static void main(String[] args) { // create character list and initialize List<Character> str = Arrays.asList('G', 'e', 'e', 'k'); // convert in string // use join() method String string = Joiner.on("").join(str); // print string System.out.println(string); } } |
Geek
3. Using List.toString(), String.substring() and String.replaceAll() method:
The toStrin() method on a list returns a string that is surrounded by square brackets and has commas between items. The idea is to get rid of square brackets using substring() method, and comma and space replace by using replaceAll() method.
Implement the above method:
// Java program for convert character list to string import java.util.Arrays; import java.util.List; // Convert List of Characters to String in Java class GFG { public static void main(String[] args) { // create character list and initialize List<Character> str = Arrays.asList('G', 'e', 'e', 'k'); // convert in string // remove [] and spaces String string = str.toString() .substring(1, 3 * str.size() - 1) .replaceAll(", ", ""); // print string System.out.println(string); } } |
Geek
4. using Collectors:
In java 8 we can make use of stream API with collectors.
Implement the above method:
// Java program for convert character list to string import java.util.stream.Collectors; import java.util.Arrays; import java.util.List; // Convert List of Characters to String in Java class GFG { public static void main(String[] args) { // create character list and initialize List<Character> str = Arrays.asList('G', 'e', 'e', 'k'); // convert in string // using collect and joining() method String string = str.stream().map(String::valueOf).collect(Collectors.joining()); // print string System.out.println(string); } } |
Geek
Recommended Posts:
- Convert a String to a List of Characters in Java
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- Convert a List of String to a comma separated String in Java
- Convert String into comma separated List in Java
- Convert an ArrayList of String to a String array in Java
- Convert a Set of String to a comma separated String in Java
- Convert List to Set in Java
- Program to Convert Set to List in Java
- Program to Convert List to Map in Java
- Convert an Iterator to a List in Java
- Convert Set of String to Array of String in Java
- Program to convert Array to List in Java
- Program to Convert List to Stream in Java
- Program to Convert a Vector to List in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



