Java

How to Convert an ArrayList to a String in Java

A String is a data type used to store a group of string values, whereas an ArrayList is a data structure that is part of the collection framework and is utilized to store the group of objects. While programming in Java, you might need to change an ArrayList to a String. Unfortunately, no such method can directly perform this conversion. However, there are some other approaches that Java supports for the specified purpose.

This post will define the Java conversion procedure from an ArrayList to a String.

How to Convert an ArrayList to a String in Java?

For converting an ArrayList to a String, you can use:

  • + operator
  • append() method
  • toString() method
  • join() method

Let’s go through the working of these methods to convert ArrayList to a String.

Method 1: Convert an ArrayList to a String Using + Operator

Converting an ArrayList to a String using the “+” operator is the easiest and simple method. You can concatenate the elements of the ArrayList as a String with the help of this concatenation operation.

Syntax
For converting ArrayList to String, follow the below syntax for the “+” operator:

str += f +",";

Here, “str” is a String type variable created to store the elements of the array as a String, “+=” operator is used to assign and concatenate the elements of the ArrayList as a String, “f” is the variable used in the for loop to access the elements and “,” is concatenated with the string elements with comma separation.

Example
In this example, first, we will create an empty String type ArrayList named “flowers”:

List<String> flowers = new ArrayList<>();

Then, add the elements in the list using the “add()” method:

flowers.add("Rose");
flowers.add("Jasmin");
flowers.add("Lily");

Print the list of “flowers”:

System.out.println("The List of Flowers:" + flowers);

Convert the list elements into String with the help of the “+” operator in the “for” loop:

String str = "";
for (String f : flowers) {
    str += f +",";
}

Finally, print the “str” to show the elements of ArrayList as a String:

System.out.print("The arrayList to string: " + str);

The output indicates the ArrayList is successfully converted into comma-separated String:

Let’s see the next method for converting ArrayList to String.

Method 2: Convert an ArrayList to a String Using append() Method

For converting ArrayList to a String, we will now use the “append()” method that belongs to the StringBuilder class. It is similar to the “+” operator as it concatenates the elements with the Strings.

Syntax
For the “append()” method, you can use the below-given syntax:

str.append(f);

Here, “str” is the object of the StringBuilder class that calls the “append()” method by passing the list, and “f” represents the ArrayList elements accessed using the for loop.

Example
In this example, we will convert the previously created ArrayList to a String using the append() method. To do so, first we will create an object “str” of the StringBuilder class:

StringBuilder str = new StringBuilder();

Then, convert the ArrayList into Strings with the help of the append() method in “for” loop:

for (String f : flowers) {
    str.append(f);
    str.append(",");
}

Lastly, print “str” that stores the elements of ArrayList as a comma separated String:

System.out.print("The arrayList to string: " + str);

Output

Let’s move to another method for converting ArrayList to String.

Method 3: Convert an ArrayList to a String Using toString() Method

In this section, we will utilize the “toString()” method of the String class for the conversion of ArrayList to String. It is a predefined method in Java that outputs the String format value. The replace() method is utilized with the toString() method to replace the particular characters.

Syntax
Follow the given syntax to convert ArrayList to a String with the toString() method:

arrayList.toString();

The “arrayList” will be converted into a String using “toString()” method.

Example
Firstly, we will create a String type variable “s” that stores the ArrayList elements as a String after performing the conversion with the toString() method:

 String s = flowers.toString();

Use the replace() method to replace the opening “[“ and closing “]” brackets and spaces between elements with empty Strings to get the comma separated String values:

 s= s.replace("[", "")
       .replace("]", "")
       .replace(" ", "");

Finally, print the resultant String on the console window:

System.out.print("The arrayList to string: " + s);

Output

Method 4: Convert an ArrayList to a String Using join() Method

join()” is a static method of the String class that joins the elements of ArrayList as String in a String type variable.

Syntax
The following syntax is used for the join() method:

String.join(",", arrayList)

Here, the join() method takes two arguments, one is the “arrayList” and the “,” that join the Strings with comma separated values.

Example
In this example, we will call the join() method with String class by passing “flowers” ArrayList and the comma “,” for separating the values and storing it in a String type variable “s”:

String s = String.join(",", flowers);

Print the resultant String on the console by utilizing the “System.out.println()” method:

System.out.print("The arrayList to string: " + s);

Output

We have compiled the easiest ways for converting an ArrayList to a String in Java.

Conclusion

For converting an ArrayList to a String, there are different methods such as using the “+” operator, append() method, toString() method, and join() method. These methods convert the elements of the ArrayList to comma-separated String values. However, utilizing the + operator is the easiest approach to perform the specified conversion. In this blog, we explained the ways of converting an ArrayList to a String in Java with detailed examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.