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 a best approach if you have other than String Array,List.We can add elements in the object of StringBuilder using 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:
Converted String is Mary,Martin,John,Newton,
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:
Converted String is Mary,Martin,John,Newton,
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); } } |
That’s all about converting List to String in java.
Join Our News Letter – Stay Updated
Subscribe to Awesome Java Content.