Java Convert ArrayList to String
In this Java core tutorial we learn how to convert a java.util.ArrayList object to a String value with different solutions in Java programming language.
How to convert ArrayList to String in Java
In the first solution we simply use the toString() method to convert an ArrayList object to a String value as the example Java code below.
ConvertArrayListToStringExample1.java
import java.util.ArrayList;
import java.util.List;
public class ConvertArrayListToStringExample1 {
public static void main(String... args) {
List<String> arrayList = new ArrayList<>();
arrayList.add("Java");
arrayList.add("Kotlin");
arrayList.add("Groovy");
String stringValue = arrayList.toString();
System.out.println(stringValue);
}
}
[Java, Kotlin, Groovy]
In the following second solution we use a for-each loop to traverse through the ArrayList and using StringBuilder to build the String result.
ConvertArrayListToStringExample2.java
import java.util.ArrayList;
import java.util.List;
public class ConvertArrayListToStringExample2 {
public static void main(String... args) {
List<String> arrayList = new ArrayList<>();
arrayList.add("Java");
arrayList.add("Kotlin");
arrayList.add("Groovy");
StringBuilder stringBuilder = new StringBuilder();
for(String item: arrayList) {
stringBuilder.append(item);
stringBuilder.append(" ");
}
String stringValue = stringBuilder.toString();
System.out.println(stringValue);
}
}
Java Kotlin Groovy
Happy Coding 😊
Related Articles
Java Convert ArrayList to Comma Separated String
Java Convert ArrayList to HashSet
Java Convert ArrayList to LinkedList
Java Convert ArrayList to Array
Java Create New Array with Class Type and Length
Java Create ArrayList using List Interface
Java Get Index of Minimum Value in ArrayList
How to use Java ArrayList with Examples
Java Convert Array to ArrayList
Java Insert Element to ArrayList at Specified Index
Java Get Minimum Value in ArrayList
How to Traverse ArrayList using Iterator in Java
How to Traverse ArrayList using for each loop in Java