Skip to content
Related Articles

Related Articles

Improve Article
Convert List of Characters to String in Java
  • Difficulty Level : Medium
  • Last Updated : 11 Dec, 2018

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);
    }
}
Output:
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);
    }
}
Output:
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);
    }
}
Output:
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);
    }
}
Output:
Geek

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :