Convert List of Characters to String in Java - GeeksforGeeks

29 Sep, 2021
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.
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
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 stringimport java.util.Arrays;import java.util.List; // Convert List of Characters to String in Javaclass 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
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 stringimport com.google.common.base.Joiner;import java.util.*; // Convert List of Characters to String in Javaclass 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
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 stringimport java.util.*; // Convert List of Characters to String in Javaclass 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
In java 8, we can make use of stream API with collectors.Implement the above method: 
Java
// Java program for convert character list to stringimport java.util.*;import java.util.stream.Collectors; // Convert List of Characters to String in Javaclass 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
nishkarshgandhi
java-list
Java-List-Programs
Java-String-Programs
Java-StringJoiner
Java-Strings
Picked
Technical Scripter 2018
Java
Technical Scripter
Java-Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
HashMap in Java with Examples
Initialize an ArrayList in Java
Interfaces in Java
ArrayList in Java
Multidimensional Arrays in Java
Stack Class in Java
Singleton Class in Java
LinkedList in Java
Collections in Java
Set in Java