TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Convert ArrayList to String

Convert an ArrayList into a string with the String.join method. Use StringBuilder and handle ints.
ArrayList, String. We often use ArrayLists to store data in Java programs. We add strings and Integers to these collections. We can convert these lists into strings.ArrayList
Approaches. For an ArrayList of Strings, we can use String.join. But for other types like Integers, a StringBuilder is a clearer approach. We can append and add delimiters.
Example, String.join. Here we have an ArrayList collection that contains String elements. We add three simple strings to it (cat, dog and bird).

Then: We use String.join to convert the ArrayList into a single string. We use a comma delimiter.

Result: The ArrayList's contents are now represented in a string with comma character delimiters.

Java program that converts ArrayList to string import java.util.ArrayList; public class Program { public static void main(String[] args) { // Create an ArrayList and add three strings to it. ArrayList<String> arr = new ArrayList<>(); arr.add("cat"); arr.add("dog"); arr.add("bird"); // Convert the ArrayList into a String. String res = String.join(",", arr); System.out.println(res); } } Output cat,dog,bird
Join, empty delimiter. Sometimes we want to combine just the strings in an ArrayList, with no delimiters between them. We use an empty string literal as the delimiter.
Java program that uses String.join, empty delimiter import java.util.ArrayList; public class Program { public static void main(String[] args) { // Append three Strings to the ArrayList. ArrayList<String> list = new ArrayList<>(); list.add("abc"); list.add("DEF"); list.add("ghi"); // Join with an empty delimiter to concat all strings. String result = String.join("", list); System.out.println(result); } } Output abcDEFghi
StringBuilder, integers. For an ArrayList containing numbers, not strings, we can convert to a string with a StringBuilder. We use a for-loop.

SetLength: We remove the last character in the StringBuilder with a call to setLength. This eliminates the trailing ":" char.

Java program that uses StringBuilder, ArrayList import java.util.ArrayList; public class Program { static String convertToString(ArrayList<Integer> numbers) { StringBuilder builder = new StringBuilder(); // Append all Integers in StringBuilder to the StringBuilder. for (int number : numbers) { builder.append(number); builder.append(":"); } // Remove last delimiter with setLength. builder.setLength(builder.length() - 1); return builder.toString(); } public static void main(String[] args) { // Create an ArrayList of three ints. ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(200); numbers.add(3000); // Call conversion method. String result = convertToString(numbers); System.out.println(result); } } Output 10:200:3000
For a simple string ArrayList, String.join is the best method to use. But for more complex objects or even numbers, a StringBuilder loop is a good option.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf