Convert ArrayList to Comma Separated String in Java
Last Updated :
18 Nov, 2021
ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. In order to convert ArrayList to a comma-separated String, these are the approaches available in Java as listed and proposed below as follows:
Earlier before Java 8 there were only standard methods available in order for this conversion but with the introduction to the concept of Streams and Lambda, new methods do arise into play of which are all listed below:
- Using append() method of StringBuilder
- Using toString() method
- Using Apache Commons StringUtils class
- Using Stream API
- Using String join() method of String class
Let us discuss each of the above methods proposed to deeper depth alongside programs to get a deep-dive understanding as follows:
Method 1: Using append() method of StringBuilder
StringBuilder in java represents a mutable sequence of characters. In the below example, we used StringBuilder’s append() method. The append method is used to concatenate or add a new set of characters in the last position of the existing string.
Syntax:
public StringBuilder append(char a)
Parameter: The method accepts a single parameter a which is the Char value whose string representation is to be appended.
Return Value: The method returns a string object after the append operation is performed.
Example:
Java
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
ArrayList<String> geeklist
= new ArrayList<String>();
geeklist.add( "Hey" );
geeklist.add( "Geek" );
geeklist.add( "Welcome" );
geeklist.add( "to" );
geeklist.add( "geeksforgeeks" );
geeklist.add( "!" );
StringBuilder str = new StringBuilder( "" );
for (String eachstring : geeklist) {
str.append(eachstring).append( "," );
}
String commaseparatedlist = str.toString();
if (commaseparatedlist.length() > 0 )
commaseparatedlist
= commaseparatedlist.substring(
0 , commaseparatedlist.length() - 1 );
System.out.println(commaseparatedlist);
}
}
|
Output
Hey,Geek,Welcome,to,geeksforgeeks,!
Method 2: Using toString() method
toString() is an inbuilt method that returns the value given to it in string format. The below code uses the toString() method to convert ArrayList to a String. The method returns the single string on which the replace method is applied and specified characters are replaced (in this case brackets and spaces).
Syntax:
arraylist.toString()
// arraylist is an object of the ArrayList class
Return Value: It returns a string representation of the ArrayList
Example:
Java
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
ArrayList<String> geekcourses
= new ArrayList<String>();
geekcourses.add( "Data Structures" );
geekcourses.add( "Algorithms" );
geekcourses.add( "Operating System" );
geekcourses.add( "Computer Networks" );
geekcourses.add( "Machine Learning" );
geekcourses.add( "Databases" );
String commaseparatedlist = geekcourses.toString();
commaseparatedlist
= commaseparatedlist.replace( "[" , "" )
.replace( "]" , "" )
.replace( " " , "" );
System.out.println(commaseparatedlist);
}
}
|
Output
DataStructures,Algorithms,OperatingSystem,ComputerNetworks,MachineLearning,Databases
Method 3: Using Apache Commons StringUtils class
Apache Commons library has a StringUtils class that provides a utility function for the string. The join method is used to convert ArrayList to comma-separated strings.
Example:
Java
import java.util.ArrayList;
import org.apache.commons.collections4.CollectionUtils;
public class GFG {
public static void main(String[] args)
{
ArrayList<String> geekcourses
= new ArrayList<String>();
geekcourses.add( "Data Structures" );
geekcourses.add( "Algorithms" );
geekcourses.add( "Operating System" );
geekcourses.add( "Computer Networks" );
geekcourses.add( "Machine Learning" );
geekcourses.add( "Databases" );
String commalist
= StringUtils.join(geekcourses, "," );
System.out.println(commalist);
}
}
|
Output:
OutputDataStructures,Algorithms,OperatingSystem,ComputerNetworks,MachineLearning,Databases
Method 4: Using Stream API
Stream API was introduced in Java 8 and is used to process collections of objects. The joining() method of Collectors Class, in Java, is used to join various elements of a character or string array into a single string object.
Example
Java
import java.util.*;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
ArrayList<String> geeklist
= new ArrayList<String>();
geeklist.add( "welcome" );
geeklist.add( "to" );
geeklist.add( "geeks" );
geeklist.add( "for" );
geeklist.add( "geeks" );
String str = geeklist.stream().collect(
Collectors.joining( "," ));
System.out.println(str);
}
}
|
Output
welcome,to,geeks,for,geeks
Method 5: Using join() method of String class
We can convert ArrayList to a comma-separated String using StringJoiner which is a class in java.util package which is used to construct a sequence of characters(strings) separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. The join() method of the String class can be used to construct the same.
Example
Java
import java.util.*;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
ArrayList<String> geeklist
= new ArrayList<String>();
geeklist.add( "welcome" );
geeklist.add( "to" );
geeklist.add( "geeks" );
geeklist.add( "for" );
geeklist.add( "geeks" );
String str = String.join( "," , geeklist);
System.out.println(str);
}
}
|
Output
welcome,to,geeks,for,geeks
Similar Reads
How to Convert a Comma-Separated String into an ArrayList in Java ?
In Java, to convert a comma-separated string into ArrayList, we can use the split() method to break the string into an array based on the comma delimiter. Another method we can also use Java Streams for a functional approach to achieve the same result. Example: Input: "Hello,from,geeks,for,geeks"Out
2 min read
Convert a List of String to a comma separated String in Java
Given a List of String, the task is to convert the List to a comma separated String in Java. Examples: Input: List<String> = ["Geeks", "ForGeeks", "GeeksForGeeks"] Output: "Geeks, For, Geeks" Input: List<String> = ["G", "e", "e", "k", "s"] Output: "G, e, e, k, s" Approach: This can be ac
1 min read
How to Convert Comma Separated String to HashSet in Java?
Given a Set of String, the task is to convert the Set to a comma-separated String in Java. Examples: Input: Set<String> = ["Geeks", "ForGeeks", "GeeksForGeeks"] Output: "Geeks, For, Geeks" Input: Set<String> = ["G", "e", "e", "k", "s"] Output: "G, e, e, k, s" There are two ways in which
2 min read
How to Convert a String to ArrayList in Java?
Converting String to ArrayList means each character of the string is added as a separator character element in the ArrayList. Example: Input: 0001 Output: 0 0 0 1 Input: Geeks Output: G e e k s We can easily convert String to ArrayList in Java using the split() method and regular expression. Paramet
1 min read
Convert a String to Character Array in Java
Converting a string to a character array is a common operation in Java. We convert string to char array in Java mostly for string manipulation, iteration, or other processing of characters. In this article, we will learn various ways to convert a string into a character array in Java with examples.
2 min read
Java Program to Convert String to Integer Array
In Java, we cannot directly perform numeric operations on a String representing numbers. To handle numeric values, we first need to convert the string into an integer array. In this article, we will discuss different methods for converting a numeric string to an integer array in Java. Example: Below
3 min read
How to Convert JSON Array to String Array in Java?
JSON stands for JavaScript Object Notation. It is one of the widely used formats to exchange data by web applications. JSON arrays are almost the same as arrays in JavaScript. They can be understood as a collection of data (strings, numbers, booleans) in an indexed manner. Given a JSON array, we wil
3 min read
How to Convert an ArrayList into a JSON String in Java?
In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList into a JSON string in Java. Steps to convert an ArrayList into a JSON str
2 min read
Convert ArrayList to Vector in Java
There are several ways to convert ArrayList to Vector. We can use a vector constructor for converting ArrayList to vector. We can read ArrayList elements one by one and add them in vector. Approach 1: (Using Vector Constructor) Create an ArrayList.Add elements in ArrayList.Create a vector and pass t
3 min read
Convert String or String Array to HashMap In Java
Considering a string object that contains the student name and roll number separated by a comma, and each student contains the name and roll number separated by a colon. Now need is to convert the String to a Map object so that each student roll number becomes the key of the HashMap, and the name be
4 min read