Convert List of Character to String in Java
This post will discuss how to convert a list of Characters to string in Java.
1. Naive solution
A simple solution is to iterate through the list and create a new string with the help of the StringBuilder class, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.List; // Convert list of Characters to string in Java class Main { public static void main(String[] args) { List<Character> chars = Arrays.asList('T', 'e', 'c', 'h', 'i', 'e'); StringBuilder sb = new StringBuilder(); for (Character ch: chars) { sb.append(ch); } String string = sb.toString(); System.out.println(string); // Techie } } |
2. Using Java 8
In Java 8, we can make use of Stream with Collectors, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; // Convert list of Characters to string in Java 8 and above class Main { public static void main(String[] args) { List<Character> chars = Arrays.asList('T', 'e', 'c', 'h', 'i', 'e'); String string = chars.stream() // Stream<Character> .map(String::valueOf) // Stream<String> .collect(Collectors.joining()); System.out.println(string); // Techie } } |
3. Using Guava Library
We can use Guava’s Joiner class to join pieces of text specified as an array and returns the results as a String. We can use this class, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import com.google.common.base.Joiner; import java.util.Arrays; import java.util.List; // Convert list of Characters to string in Java using Guava class Main { public static void main(String[] args) { List<Character> chars = Arrays.asList('T', 'e', 'c', 'h', 'i', 'e'); String string = Joiner.on("").join(chars); System.out.println(string); // Techie } } |
4. Using Apache Commons Lang
Like Guava, Apache Commons Lang StringUtils class offers a join() method that can join the specified array elements into a single string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import org.apache.commons.lang3.StringUtils; import java.util.Arrays; import java.util.List; // Convert list of Characters to string in Java using Apache Commons class Main { public static void main(String[] args) { List<Character> chars = Arrays.asList('T', 'e', 'c', 'h', 'i', 'e'); String string = StringUtils.join(chars, null); System.out.println(string); // Techie } } |
5. Using List.toString() method
(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 the substring() method and comma + space by using the replaceAll() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.List; // Convert list of Characters to string in Java class Main { public static void main(String[] args) { List<Character> chars = Arrays.asList('T', 'e', 'c', 'h', 'i', 'e'); String string = chars.toString() .substring(1, 3*chars.size()-1) .replaceAll(", ", ""); System.out.println(string); // Techie } } |
That’s all about converting List of Character to Java String.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)