Convert List to String in Java
In this post, you will learn how to convert list to string using the Java programming language. In the development process, we may face some scenarios where there may be a need to convert a list to a string.
List in Java
A list is an ordered collection. It contains index-based methods to insert, update, delete, and search the elements. We can access elements by their index and also search for elements in the list. A list can contain duplicate values and null elements. The list interface gives positional access and insertion of elements and preserves the insertion order.
List Interface declaration-public interface List extends Collection
String in Java
In Java, string is object that represent a sequence of characters. Java strings are created and manipulated through the string class. In Java, a string is immutable, which means its value cannot be changed once it is created.
Syntax-String s=new String();
Different ways to convert List to String in Java
In this post, we have used the following ways to convert a list to a string-
- Using StringBuilder class
- Using join() method of Joiner class
- Using Collectors
- Using toString() method
Convert List to String in Java using StringBuilder class
The StringBuilder in Java represents a mutable sequence of characters. The append() method of the StringBuilder class appends the string representation of the given list. In the given example, we iterate through the list elements and append each element at the end of the string. At last, the string contains the string form of the characters.
// Java program to convert list to string
import java.util.Arrays;
import java.util.List;
// Convert List of Characters to String in Java
public class ListToString {
public static void main(String[] args)
{
// Initialize character list
List str
= Arrays.asList('w','e','l','c','o','m','e');
System.out.println("List - " + str);
// Creating object of StringBuilder class
StringBuilder strBldr = new StringBuilder();
// Creating a string using append() method
for (Character ch : str) {
strBldr.append(ch);
}
String string = strBldr.toString();
System.out.println("String - " + string);
}
}
Output of the above code:
List - [w, e, l, c, o, m, e]
String - welcome
Convert List to String in Java using join() method of Joiner class
Java Joiner class provides various methods to handle joining operations on strings, objects, etc. This method is also called the Guava method. The join() method of the Joiner class joins elements of a list and returns a string containing a string representation of each of the parts using the previously configured separator between each.
// Java program to convert character list to string
import com.google.common.base.Joiner;
import java.util.*;
public class ListToStr {
public static void main(String[] args)
{
// Initializing character list
List str
= Arrays.asList('H', 'e', 'l', 'l', 'o');
System.out.println("List - " + str);
// using join() method
String string = Joiner.on("").join(str);
System.out.println("String - " + string);
}
}
Output of the above code:
List - [H, e, l, l, o]
String - Hello
Convert List to String in Java using Collectors
In Java, Collectors is a final class that extends Object class which provides reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, grouping etc. Here, we have used this class to convert a list to string.
// Java program to convert list to string
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
// Convert List to String in Java
public class ListToStr {
public static void main(String[] args)
{
// Initializing list
List list = Arrays.asList(2, 6, 4, 3, 7, 8, 4);
String sep = "-";
String res = list.stream()
.map(Object::toString)
.collect(Collectors.joining(sep));
System.out.println(res);
}
}
Output of the above code:
2-6-4-3-7-8-4
Convert List to String in Java using toString() method
Here, we use the toString() method of the list to convert the list into a string. The return string is surrounded by square brackets and has commas between items. If we want to get rid of opening or closing square brackets, we can use the substring() method. We can replace the comma and space by using the replaceAll() method.
// Java program to convert list to string
import java.util.*;
// Convert List to String in Java
public class ListToStr {
public static void main(String[] args)
{
// Initializing character list
List list = Arrays.asList('w','e','l','c','o','m','e');
String res = list.toString()
.substring(1, 3 * list.size() - 1)
.replaceAll(", ", "");
System.out.println(res);
}
}
Output of the above code:
welcome
Related Articles
Parse JSON in javaJava write data to JSON file
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number