To convert the ArrayList to a comma-separated string, these are the methods available in Java.
Method 1(Using append() Method of StringBuilder):
StringBuilder in java represents a mutable sequence of characters. within the below example, we used StringBuilder’s append() method. The append method is employed to concatenate or add a replacement set of characters within the last position of the prevailing string.
Syntax :
public StringBuilder append(char a)
Parameter:
// Java program to Convert ArrayList to Comma Separated // String import java.util.ArrayList; public class Main { public static void main(String[] args) { // Create an ArrayList ArrayList geeklist = new ArrayList(); // Add elements to ArrayList geeklist.add("Hey"); geeklist.add("Geek"); geeklist.add("Welcome"); geeklist.add("to"); geeklist.add("geeksforgeeks"); geeklist.add("!"); StringBuilder str = new StringBuilder(""); // Traversing the ArrayList for (String eachstring : geeklist) { // Each element in ArrayList is appended // followed by comma str.append(eachstring).append(","); } // StringBuffer to String conversion String commaseparatedlist = str.toString(); // By following condition you can remove the last // comma if (commaseparatedlist.length() > 0) commaseparatedlist = commaseparatedlist.substring( 0, commaseparatedlist.length() - 1); System.out.println(commaseparatedlist); } }
Output
Hey,Geek,Welcome,to,geeksforgeeks,!
Here, arraylist is an object of the ArrayList class.
Return Value: It returns a string representation of the arraylist
// Java program to Convert ArrayList to Comma Separated // String in Java import java.util.ArrayList; public class Main { public static void main(String[] args) { // Create an ArrayList ArrayList geekcourses = new ArrayList(); // Add elements to ArrayList geekcourses.add("Data Structures"); geekcourses.add("Algorithms"); geekcourses.add("Operating System"); geekcourses.add("Computer Networks"); geekcourses.add("Machine Learning"); geekcourses.add("Databases"); /* toString method returns the output as [Data Structure,Algorithms,...] In order to replace '[', ']' and spaces with empty strings to get comma separated values.*/ 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):
// Java program to Convert ArrayList to Comma Separated // String in Java import java.util.ArrayList; import org.apache.commons.collections4.CollectionUtils; public class Main { public static void main(String[] args) { // Create an ArrayList ArrayList geekcourses = new ArrayList(); // Add elements to ArrayList geekcourses.add("Data Structures"); geekcourses.add("Algorithms"); geekcourses.add("Operating System"); geekcourses.add("Computer Networks"); geekcourses.add("Machine Learning"); geekcourses.add("Databases"); /*join method of StringUtils class is used which returns a single string along with defined separator in every iteration.*/ 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 employed to process collections of objects. The joining() method of Collectors Class, in Java, is employed to hitch various elements of a personality or string array into one string object.
// Java program to Convert ArrayList to Comma Separated // String in Java import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { // Create ArrayList of String ArrayList geeklist = new ArrayList(); // add elements to ArrayList geeklist.add("welcome"); geeklist.add("to"); geeklist.add("geeks"); geeklist.add("for"); geeklist.add("geeks"); // The collect method is used to return the result // of the intermediate operations performed on the // stream. String str = geeklist.stream().collect( Collectors.joining(",")); System.out.println(str); } }
Output
welcome,to,geeks,for,geeks
Method 5(Using String join() method:
We can use StringJoiner to convert the ArrayList into a comma-separated string. StringJoiner is a class in the java.util package, used to construct a sequence of characters (strings) separated by separators, and can choose to start with the provided prefix and start with The suffix provided ends. The join() method of the String class can be used to construct the same content.
// Java program to Convert ArrayList to Comma Separated // String in Java import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { // Create ArrayList of String ArrayList geeklist = new ArrayList(); // add elements to ArrayList geeklist.add("welcome"); geeklist.add("to"); geeklist.add("geeks"); geeklist.add("for"); geeklist.add("geeks"); // String.join() is used with a delimiter comma // along with the list. String str = String.join(",", geeklist); System.out.println(str); } }
Output
welcome,to,geeks,for,geeks
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.