Convert List of Characters to String in Java
Last Updated :
31 Jul, 2023
Given a list of characters. In this article, we will write a Java program to convert the given list to a string.
Example of List-to-String Conversion
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 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.
Methods to Convert List to String in Java
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
1. Using StringBuilder class to Convert List to String
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 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');
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);
}
}
OutputList - [G, e, e, k, s]
String - Geeks
The complexity of the above method
Time Complexity: O(n)
Auxiliary Space: O(n)
2. Using join() method of Joiner Class
A 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.
Below is the implementation of the above method:
Java
// Java program for convert character list to string
import com.google.common.base.Joiner;
import java.util.*;
// 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');
System.out.println("List - " + str);
// convert in string
// use join() method
String string = Joiner.on("").join(str);
// print string
System.out.println("String - " + string);
}
}
Output
List - [G, e, e, k]
String - Geek
The complexity of the above method
Time Complexity: O(n)
Auxiliary Space: O(n)
3. Using List.toString(), String.substring() and String.replaceAll() method
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 Below implementation of the above method is given below:
Java
// Java program for convert character list to string
import java.util.*;
// 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');
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);
}
}
OutputList - [G, e, e, k]
String - Geek
The complexity of the above method:
Time Complexity: O(n)
Auxiliary Space: O(n)
4. Using Collectors in Java for List-to-String Conversion
In Java 8, we can make use of stream API with Java collectors.
Below is the implementation of the above method:
Java
// Java program for convert character list to string
import java.util.*;
import java.util.stream.Collectors;
// 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');
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);
}
}
OutputList - [G, e, e, k]
String - Geek
The complexity of the above method:
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Convert a String to a List of Characters in Java In Java, to convert a string into a list of characters, we can use several methods depending on the requirements. In this article, we will learn how to convert a string to a list of characters in Java.Example:In this example, we will use the toCharArray() method to convert a String into a character
3 min read
How to Convert Character Array to String in Java? Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character "\0". A character array can be converted to a string and vice versa. In this article, we will discuss how to convert a character array to a string
4 min read
Java Program to Get a Character from a String Given a String str, the task is to get a specific character from that String at a specific index. Examples:Input: str = "Geeks", index = 2Output: eInput: str = "GeeksForGeeks", index = 5Output: F Below are various ways to do so: Using String.charAt() method: Get the string and the indexGet the speci
5 min read
Swapping Pairs of Characters in a String in Java Given string str, the task is to write a Java program to swap the pairs of characters of a string. If the string contains an odd number of characters then the last character remains as it is. Examples: Input: str = âJavaâOutput: aJav Explanation: The given string contains even number of characters.
3 min read
Java Program to convert Character Array to IntStream Given a Character Array, the task is to convert this array into an IntStream containing the ASCII values of the character elements. Examples: Input: char[] = { 'G', 'e', 'e', 'k', 's' } Output: 71, 101, 101, 107, 115 Input: char[] = { 'G', 'e', 'e', 'k', 's', 'F', 'o', 'r', 'G', 'e', 'e', 'k', 's' }
1 min read