introduction
How to convert List to String in Java. Next, use the Java 8 Streams Collectors api and () method to convert the collection with comma separators or custom separators into a string. This conversion is done through simple steps using the java api method. First, learn how to convert List to String using the toString() method, and finally learn the famous apache library command() method.
For all examples, the input list must be of String as the List type, otherwise we need to convert a non-string to a String. For example, List is of type Double, and then you need to convert double to string first.
Use the standard toString() method
() is the easiest, but it adds square brackets at the beginning and end, each string is separated by a comma separator.
The disadvantage is that we cannot replace commas with another separator, nor can we remove square brackets.
import ; import ; public class ListToStringUsingToStringExample { public static void main(String args) { // creating a list with strings. List<String> list = ("One", "Two", "Three", "Four", "Five"); // converting List<String> to String using toString() method String stringFromList = (); // priting the string ("String : "+stringFromList); } } Output: String : [One, Two, Three, Four, Five]
Java 8 ()
java 8 String adds a special method() to convert the collection into a string with a given delimiter.
public class ListToStringUsingString_JoinExample { public static void main(String args) { // creating a list with strings. List<String> list = ("One", "Two", "Three", "Four", "Five"); // converting List<String> to String using toString() method String stringFromList = ("~", list); // priting the string ("String with tilde delimiter: "+stringFromList); // delimiting with pipe | symbol. String stringPipe = ("|", list); // printing ("String with pipe delimiter : "+stringPipe); } } Output: String with tilde delimiter: One~Two~Three~Four~Five String with pipe delimiter : One|Two|Three|Four|Five
()
The () method comes from the java 8 stream api. The () method takes the delimiter, prefix and suffix as parameters. This method converts the list to a string with the given delimiter, prefix, and suffix.
Check out the following examples of the join() method using different delimiters. However, the () method does not provide prefix and suffix options.
Use these if you need custom delimiters, prefixes, and suffixes. If you don't want prefixes and suffixes, provide an empty string to not add anything before and after the result string.
public class ListToStringUsingString_JoinExample { public static void main(String args) { // creating a list with strings. List<String> list = ("One", "Two", "Three", "Four", "Five"); // using java 8 with delimiter, prefix and suffix String joiningString = ().collect(("-", "{", "}")); // printing (" string : "+joiningString); String joiningString3 = ().collect(("@", "", "")); // printing (" string with @ separator : "+joiningString3); } } Output: string : {One-Two-Three-Four-Five} string with @ separator : One@Two@Three@Four@Five
Apache Commons ()
The final approach is to use an external library from the apache commons package. The library has a method() which takes a list and delimiter similar to the () method.
public class ListToStringUsingStringUtils_JoinExample { public static void main(String args) { // creating a list with strings. List<String> list = ("One", "Two", "Three", "Four", "Five"); // using java 8 with delimiter, prefix and suffix String joiningString = (list, "^"); // printing (" string with ^ delimiter : "+joiningString); String joiningString3 = (list, "$"); // printing (" string with @ separator : "+joiningString3); } } Output: string with ^ delimiter : One^Two^Three^Four^Five string with @ separator : One$Two$Three$Four$Five
in conclusion
In this article, we have seen how to convert List to String in java using different methods before and after java 8. It is nice to use the () method for a given delimiter to generate strings from List. Alternatively, if you want to add a prefix or suffix, use the stream api () method with delimiter, prefix, and suffix values.
The above is the detailed content of three methods in Java to convert List lists into strings. For more information about converting Java List lists to strings, please pay attention to my other related articles!