Convert List of Characters to String in Java

In this post, we will discuss how to convert List of Characters to String in Java.


 

1. Naive

Simple solution would be to iterate through the list and create a new string with the help of StringBuilder class as shown below:

 

Download   Run Code

Output:

Techie

 

2. Java 8

In Java 8, we can make use of Stream API with Collectors as shown below:

 

Download   Run Code

Output:

Techie

 

3. Guava

Guava’s Joiner class can be used to join pieces of text specified as an array, and returns the results as a String. We can use this class as shown below:

 

Download   Run Code

Output:

Techie

 

4. Apache Commons Lang

Similar to Guava, Apache Commons Lang StringUtils class offers a join() method that can joins the elements of the specified array into a single String.

 

Download

Output:

Techie

 

5. List.toString() + String.substring() + String.replaceAll()

(Not recommended)

We can get a string representation of the List elements, which are enclosed in square brackets ("[]") and all adjacent elements are separated by a comma followed by a space ", ". The idea is to get rid of the square brackets using substring() function, and comma + space by using replaceAll() method.

 

Download   Run Code

Output:

Techie

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 


Get great deals at Amazon




Leave a Reply

Notify of
avatar
Sort by:   newest | oldest | most voted
abdullah
Guest
abdullah

thanks

wpDiscuz