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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Geek

4. using Collectors:
In java 8 we can make use of stream API with collectors.

Implement the above method:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Geek


My Personal Notes arrow_drop_up

Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

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.