How to convert list to String in Java
Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without problems. We have a few unique strings, consisting of a palindrome string (a string that can be the same after reversing). To reverse a given string, we need to change the String to a character array of all the characters of the String, and after reversing the char array, we need to transform it into a string.
We can convert a list to a string in java in many ways, some are as follows:
1. Using StringBuilder class
Although, there are many ways to transform a list into a string, by using the StringBuilder class, it can be done easuly. The List can be iterated using for loop and generate a new string with the assistance of StringBuilder.
By using a simple program, we can implement the code for transforming a list into a string by using the StringBuilder class:
Example
// import of the packages
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// create class ConvertListToString which is used for converting the input char list to string
class ConvertListToString{
// main method()
public static void main(String[] args)
{
// decalaring variabes and list to store characters
int sizeofList;
String str;
List<Character> charList = new ArrayList<Character>();
// scanner class created for taking input from user
Scanner sc = new Scanner(System.in);
System.out.println("Enter the list size.");
sizeofList= sc.nextInt();
for(int i = 0; i < sizeofList; i++) {
System.out.println("ENTER THE char["+i+"]: ");
charList.add(sc.next().charAt(0));
}
// closing of the scanner class
sc.close();
// displaying the list of character elements
System.out.println("List: " + charList);
// An object is created for string builder class
StringBuilder builder = new StringBuilder();
//visiting each character in the character list
for (Character cha : charList) {
builder.append(cha);
}
// object is converted into string
str = builder.toString();
//String is displayed
System.out.println("List is coverted to string: " + str);
}
}
Output

2: Using List.toString(), String.substring() and String.replaceAll() methods
Converting the List into the String, we use these 3 methods. The following three methods we use the following operations:
1. The toString () method converts the List into a String. The return value will contain square brackets and commas(,).
2. The substring() method can escape from opening or closing square brackets.
3. replace() all method is used to replace all spaces and commas.
We can implement a code for converting lists to strings using these methods.
Example
// required packages are imported
import java.util.ArrayList; //reading list elements
import java.util.List;
import java.util.Scanner;
// A class listtostring is created to convert list into string by using all the three methods
class Main {
// start of main() method
public static void main(String[] args)
{
//declaring variables and list
int size;
String str;
List<Character> charList = new ArrayList<Character>();
// Scanner class is used for reading dynamic input from user
Scanner sc = new Scanner(System.in);
System.out.println("Enter the list.");
size = sc.nextInt();
for(int i = 0; i < size; i++) {
System.out.println("ENTER char["+i+"]: ");
charList.add(sc.next().charAt(0));
}
// scanner class object is closed
sc.close();
// displaying the character list elements
System.out.println("List: " + charList);
// used for converting list to string by three methods
str = charList.toString()
.substring(1, 3 * charList.size() - 1)
.replaceAll(", ", "");
//String displaying
System.out.println("List is converted from string: " + str);
}
}
Output

3. Collectors in java
It is also one of the ways to convert a given list into a string. Stream collectors are used for this method, which is available under the version of Java8.
This can be implemented by a simple program using API collectors in java.
Example
// packages import section
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
// class ConvertListToString is created to convert list to string using api collectors
class ConvertListToString {
// main method
public static void main(String[] args)
{
// variables and list are declared
int size;
String str;
List<Character> charList = new ArrayList<Character>();
// Scanner class is created for taking input at runtime
Scanner c = new Scanner(System.in);
System.out.println("Enter the size of the list.");
size = c.nextInt();
for(int i = 0; i < size; i++) {
System.out.println("ENTER char["+i+"]: ");
charList.add(c.next().charAt(0));
}
// Ending of scanner class object
c.close();
// displaying the elements of the character list
System.out.println("List: " + charList);
// converting of list to string by using collect() method
str = charList.stream()
.map(String::valueOf)
.collect(Collectors.joining());
// the output string is displayed
System.out.println("Converted string: " + str);
}
}
Output

Hence in these ways, a list can be converted to a string. Among these, one of the most used ways are the StringBuffer method from which a List can be converted to String. Additionally, you can use any method accordingly.