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
import java.util.Arrays;
import java.util.List;
class GFG {
public static void main(String[] args)
{
List<Character> str
= Arrays.asList( 'G' , 'e' , 'e' , 'k' , 's' );
System.out.println( "List - " + str);
StringBuilder sb = new StringBuilder();
for (Character ch : str) {
sb.append(ch);
}
String string = sb.toString();
System.out.println( "String - " + string);
}
}
|
Output
List - [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
import com.google.common.base.Joiner;
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Character> str
= Arrays.asList( 'G' , 'e' , 'e' , 'k' );
System.out.println( "List - " + str);
String string = Joiner.on( "" ).join(str);
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
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Character> str
= Arrays.asList( 'G' , 'e' , 'e' , 'k' );
System.out.println( "List - " + str);
String string
= str.toString()
.substring( 1 , 3 * str.size() - 1 )
.replaceAll( ", " , "" );
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)
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
import java.util.*;
import java.util.stream.Collectors;
class GFG {
public static void main(String[] args)
{
List<Character> str
= Arrays.asList( 'G' , 'e' , 'e' , 'k' );
System.out.println( "List - " + str);
String string = str.stream()
.map(String::valueOf)
.collect(Collectors.joining());
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)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!