String in ArrayList Java

  1. HowTo
  2. Java Howtos
  3. Convert an Arraylist to a String in Java

Convert an Arraylist to a String in Java

Java Java String Java ArrayList

Created: October-02, 2020 | Updated: March-24, 2021

This tutorial introduces how to convert an ArrayList to a string in Java and lists some example codes to understand the topic. There are several ways to convert ArrayList like using join[], append[] method, StringUtils class, etc. Lets take a close look at the examples.

Convert ArrayList to String With + Operator in Java

The most straightforward and easy way to convert an ArrayList to String is to use the plus [+] operator. In String, the plus operator concatenates two string objects and returns a single object. Here, we are using the plus operator. See the example below.

import java.util.ArrayList; import java.util.List; public class SimpleTesting { public static void main[String[] args] { // ArrayList List fruits = new ArrayList[]; fruits.add["Apple"]; fruits.add["Orange"]; fruits.add["Mango"]; System.out.println[fruits]; String str = ""; for [String fruit : fruits] { str+= fruit+","; } System.out.println[str]; } }

Output:

[Apple, Orange, Mango] Apple,Orange,Mango,

Convert ArrayList to String Using the append[] Method in Java

We can use the append[] method of StringBuilder class to convert ArrayList to string. This method returns a String object. See the example below.

import java.util.ArrayList; import java.util.List; public class SimpleTesting { public static void main[String[] args] { // ArrayList List fruits = new ArrayList[]; fruits.add["Apple"]; fruits.add["Orange"]; fruits.add["Mango"]; System.out.println[fruits]; StringBuilder str = new StringBuilder[]; for [String fruit : fruits] { str.append[fruit]; str.append[" "]; } System.out.println[str]; } }

Output:

[Apple, Orange, Mango] Apple Orange Mango

Convert ArrayList to String Using the join[] Method in Java

We can use the join[] method of the String class to join ArrayList elements into a single string. This method returns a string and can be used to convert ArrayList to String in Java.

import java.util.ArrayList; import java.util.List; public class SimpleTesting { public static void main[String[] args] { // ArrayList List fruits = new ArrayList[]; fruits.add["Apple"]; fruits.add["Orange"]; fruits.add["Mango"]; System.out.println[fruits]; String str = String.join[",", fruits]; System.out.println[str]; } }

Output:

[Apple, Orange, Mango] Apple,Orange,Mango

Convert ArrayList to String Using StringUtils Class in Java

If you are working with the Apache commons lang library, then you can use the join[] method of StringUtils class to get string object from the ArrayList. Here, we use the join[] method that takes two arguments: ArrayList and the separator symbol. See the example below.

import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; public class SimpleTesting { public static void main[String[] args] { // ArrayList List fruits = new ArrayList[]; fruits.add["Apple"]; fruits.add["Orange"]; fruits.add["Mango"]; System.out.println[fruits]; String str = StringUtils.join[fruits, ", "]; System.out.println[str]; } }

Output:

[Apple, Orange, Mango] Apple, Orange, Mango

Convert ArrayList to String Using the replaceAll[] Method in Java

This is a little tricky approach to get the string from ArrayList. In this way, we use the replaceAll[] method to replace square brackets of the List and then convert all elements into String using the toString[] method. See the example below.

import java.util.ArrayList; import java.util.List; public class SimpleTesting { public static void main[String[] args] { // ArrayList List fruits = new ArrayList[]; fruits.add["Apple"]; fruits.add["Orange"]; fruits.add["Mango"]; System.out.println[fruits]; String str = fruits.toString[].replaceAll["\\[|\\]", ""].replaceAll[", ",", "]; System.out.println[str]; } }

Output:

[Apple, Orange, Mango] Apple, Orange, Mango

Convert ArrayList to String Using the joining[] Method in Java

If you are using Java 8 or higher version, you can use the joining[] method of Collectors class to collect all the elements into a single String object. Here, we use the functional style of Java programming added into Java 8 version.

import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class SimpleTesting { public static void main[String[] args] { // ArrayList List fruits = new ArrayList[]; fruits.add["Apple"]; fruits.add["Orange"]; fruits.add["Mango"]; System.out.println[fruits]; String str = Arrays.asList[fruits].stream[] .map[Object::toString] .collect[Collectors.joining[", "]]; System.out.println[str]; } }

Output:

[Apple, Orange, Mango] [Apple, Orange, Mango]

Convert ArrayList to String Using the deepToString[] Method in Java

We can use the deepToString[] method of Arrays class that converts all the elements of ArrayList into a String object. See the example below.

import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class SimpleTesting { public static void main[String[] args] { // ArrayList List fruits = new ArrayList[]; fruits.add["Apple"]; fruits.add["Orange"]; fruits.add["Mango"]; System.out.println[fruits]; String str = Arrays.deepToString[fruits.toArray[]]; System.out.println[str]; } }

Output:

[Apple, Orange, Mango] [Apple, Orange, Mango]
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java String

  • Split a String by Space in Java
  • Convert a String to Char in Java

    Related Article - Java ArrayList

  • Count Characters in a String in Java
    • Round a Double to Two Decimal Places in Java
    • Print a Float With 2 Decimal Places in Java
    report this ad

    Video liên quan

    Chủ Đề