Java Convert ArrayList to String

Tags: ArrayList 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);
    }
}
The output as below.
[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);
    }
}
The output as below.
Java Kotlin Groovy

Happy Coding 😊

Java Convert ArrayList to Comma Separated String

Java Convert ArrayList to HashSet

Java Convert ArrayList to LinkedList

Java Convert ArrayList to Array

Convert Array to List in Java

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

How to Traverse ArrayList using forEach() method in Java

Java Add Element to ArrayList