In this post, we will see how to convert List to String in java.
Following are the ways to convert list into String:
Table of Contents
Using StringBuilder
We can use StringBuilder class to convert List to String. StringBuilder is the best approach if you have other than String Array, List.We can add elements in the object of StringBuilder using the append()
method while looping and then convert it into string using toString()
method of String class at the end.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package org.arpit.java2blog; import java.util.List; import java.util.ArrayList; import java.lang.String; public class ConvertListToStringMain { public static void main(String arg[]) { //creation and initialization of list List<String> list=new ArrayList<>(); list.add("Mary"); list.add("Martin"); list.add("John"); list.add("Newton"); //print content of list System.out.print("Elements of list are:"); System.out.println(list); //conversion of ArrayList to String StringBuilder strbul=new StringBuilder(); for(String str : list) { strbul.append(str); //for adding comma between elements strbul.append(","); } //just for removing last comma //strbul.setLength(strbul.length()-1); String str=strbul.toString(); System.out.println("Converted String is " + str); } } |
OUTPUT:
Using + operator
This is not an efficient way so, normally avoid it.We use concate(+
) operator to add one by one element from array inside loop and assign it into String.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package org.arpit.java2blog; //import all these packages import java.util.List; import java.util.ArrayList; import java.lang.String; import java.util.Arrays; public class ListToStringPlusMain { public static void main(String arg[]) { //creation and initialization of list List list=new ArrayList<>(); list.add("Mary"); list.add("Martin"); list.add("John"); list.add("Newton"); //print content of list System.out.print("Elements of list are:"); System.out.println(list); //conversion of ArrayList into String using + String str=""; for(String s : list) { str=str + s + ","; } System.out.println("Converted String is " + str); } } |
OUTPUT:
Using String’s join method in java 8
String’s join method is introduced in Java 8 . This method is very handy to create delimited separated String from list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package org.arpit.java2blog; //import all these packages import java.util.List; import java.util.ArrayList; import java.lang.String; public class ListToStringJoinMain { public static void main(String arg[]) { //creation and initialization of list List list=new ArrayList<>(); list.add("Mary"); list.add("Martin"); list.add("John"); list.add("Newton"); //print content of list System.out.print("Elements of list are:"); System.out.println(list); //conversion of ArrayList into String using + String str=String.join(",", list); System.out.println("Converted String is " + str); } } |
This approach will work only for List of String. In case, if you have list of Double, then you can use java 8‘s Collectors to join the String.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class ConvertListToStringMain { public static void main(String arg[]) { //creation and initialization of list List<Double> list=new ArrayList<>(); list.add(34.2); list.add(29.7); list.add(40.2); list.add(50.4); //print content of list System.out.print("Elements of list are:"); System.out.println(list); //conversion of ArrayList into String using Java 8's Collector String str = list.stream().map(Object::toString) .collect(Collectors.joining(",")); System.out.println("Converted String is: " + str); } } |
Output:
As you can see, we have used Collectors.joining(",")
to join elements of String by ,
Using Apache common lang3
You can also use Apache common’s StringUtils
to convert list to String.
Here is the dependency which you need to add for Apache common lang3 in pom.xml
.
1 2 3 4 5 6 7 |
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> |
Here is the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.List; import org.apache.commons.lang3.StringUtils; public class ConvertStringToListApache { public static void main(String arg[]) { List<Integer> intList= Arrays.asList(new Integer[] {1,2,3,4}); //print content of list System.out.print("Elements of list are:"); System.out.println(intList); String join = StringUtils.join(intList,"*"); System.out.println("Converted String is: " + join); } } |
Using Guava library
You can also use Guava’s Joiner
class to convert list to String.
Here is the dependency which you need to add for guava in pom.xml
.
1 2 3 4 5 6 7 |
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>29.0-jre</version> </dependency> |
Here is the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.List; import com.google.common.base.Joiner; public class ConvertListToStringGuava { public static void main(String arg[]) { List<Integer> intList= Arrays.asList(new Integer[] {1,2,3,4}); //print content of list System.out.print("Elements of list are:"); System.out.println(intList); String join = Joiner.on("*").join(intList); System.out.println("Converted String is: " + join); } } |
As you can see, we have used com.google.common.base.Joiner
class to join elements of String by *
Print elements of list as String
Sometimes, we just need to print elements of the list. We can simply use toString()
method
Let’s understand with the help of example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.List; public class PrintListOfElementsMain { public static void main(String arg[]) { List<Integer> intList= Arrays.asList(new Integer[] {1,2,3,4}); //print content of list System.out.print("Elements of list are:"); System.out.println(intList); } } |
Output:
Did you notice we did not even call toString()
method on intList object?
JVM internally calls toString() method of type of element within this list. In this case, we have list of Integers, so it will call Integer’s toString()
method.
In case, you have custom object, then you should override toString() method, else it will give you unexpected results.
Let’s see with the help of example
Create a simple class named Color.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.java2blog; public class Color { String name; String htmlCode; public Color(String name, String htmlCode) { super(); this.name = name; this.htmlCode = htmlCode; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getHtmlCode() { return htmlCode; } public void setHtmlCode(String htmlCode) { this.htmlCode = htmlCode; } } |
Create main class named PrintListOfColorsMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class PrintListOfColorsMain { public static void main(String[] args) { // Sort list of colors by name List<Color> listOfColors = getListOfColors(); System.out.println(listOfColors); } public static List<Color> getListOfColors() { List<Color> listOfColors = new ArrayList<>(); Color red = new Color("Red", "#FF0000"); Color blue = new Color("Blue", "0000FF"); Color white = new Color("White", "#FFFFFF"); Color green = new Color("Green", "#008000"); listOfColors.add(red); listOfColors.add(blue); listOfColors.add(white); listOfColors.add(green); return listOfColors; } } |
Output:
If you notice, we don’t have toString()
method in Color
class, that’s why it is calling Object’s toString() method.
We need to override toString()
method in Color
class and we will get informative output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package org.arpit.java2blog; public class Color { String name; String htmlCode; public Color(String name, String htmlCode) { super(); this.name = name; this.htmlCode = htmlCode; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getHtmlCode() { return htmlCode; } public void setHtmlCode(String htmlCode) { this.htmlCode = htmlCode; } @Override public String toString() { return "Color [name:"+name+" HtmlCode:"+htmlCode+"]"; } } |
When you run PrintListOfColorsMain
again, you will get below output:
As you can see, we have much meaningful output now.
That’s all about converting List to String in java.