Convert List of Characters to String in Java
Given a list of characters. Write a Java program to convert the given list to a string.
Input : list = {'g', 'e', 'e', 'k', 's'} Output : "geeks" Input : list = {'a', 'b', 'c'} Output : "abc"
Strings – Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are also a type of exceptional array that holds characters, therefore, strings are immutable as well.
List – List in Java implements the ability to manage the ordered collection. It comprises the index-based techniques to insert, update, delete and search the elements of the list. It can have duplicate elements also. The List interface is located in java.util package and inherits the Collection interface.
Approaches
There are numerous approaches to convert the List of Characters to String in Java. These are –
- Using StringBuilder class
- Using join() method of Joiner class
- Using List.toString(), String.substring() and String.replaceAll() method
- Using Collectors
1. Using StringBuilder class
A simple solution would be to iterate through the list and create a new string with the help of the StringBuilder class, as shown below:
Java
// 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' ); System.out.println( "List - " + str); // 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 - " + string); } } |
List - [G, e, e, k, s] 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 is also called the Guava method.
Java
// Java program for convert character list to string import com.google.common.base.Joiner; import java.util.*; // 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' ); System.out.println( "List - " + str); // convert in string // use join() method String string = Joiner.on( "" ).join(str); // print string System.out.println( "String - " + string); } } |
List - [G, e, e, k] String - Geek
3. Using List.toString(), String.substring() and String.replaceAll() method
The toString() 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 the substring() method and comma and space replace using the replaceAll() method.
The implementation of the above method is given below –
Java
// Java program for convert character list to string import java.util.*; // 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' ); System.out.println( "List - " + str); // convert in string // remove [] and spaces String string = str.toString() .substring( 1 , 3 * str.size() - 1 ) .replaceAll( ", " , "" ); // print string System.out.println( "String - " + string); } } |
List - [G, e, e, k] String - Geek
4. Using Collectors in Java
In java 8, we can make use of stream API with collectors.
Implement the above method:
Java
// Java program for convert character list to string import java.util.*; import java.util.stream.Collectors; // 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' ); System.out.println( "List - " + str); // convert in string // using collect and joining() method String string = str.stream() .map(String::valueOf) .collect(Collectors.joining()); // print string System.out.println( "String - " + string); } } |
List - [G, e, e, k] String - Geek