top of page

How to convert ArrayList to String in Java - Spring Example


Some time we need to convert ArrayList to String in Java programming language in order to pass that String to stored procedure, any method or any other program. Unfortunately Java collection framework doesn't provide any direct utility method to convert ArrayList to String in Java. But Spring framework which is famous for dependency Injection and its IOC container also provides API with common utilities like method to convert Collection to String in Java. You can convert ArrayList to String using Spring Framework's StringUtils class. StringUtils class provide three methods to convert any collection e.g. ArrayList to String in Java, as shown below:

public static String collectionToCommaDelimitedString(Collection coll)
public static String collectionToDelimitedString(Collection coll, String delim)
public static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)

By using above method you can convert ArrayList to comma separated String, pipe separated String or you can provide any other separator. You can also check How to convert Collection to String for more examples of these methods.


package test;

import java.util.ArrayList;
import org.springframework.util.StringUtils;

/**
 *
 * Java program to convert ArrayList to String in Java using Spring framework.
 * It uses Spring's StringUtils class to create String from ArrayList in Java
 * @author
 */
public class ArrayListProgram {

 
 public static void main(String args[]) {

 // ArrayList to be converted into String
 ArrayList<String> language = new ArrayList<String>();
        language.add("Java");
        language.add("C++");
        language.add("Scala");
 
 // Converting ArrayList to String using Spring API , result is comma separated String      
 String arraylistToString = StringUtils.collectionToCommaDelimitedString(language);
 
 System.out.println("String converted from ArrayList : " + arraylistToString);
 
 //If you want to use separater other than coma while converting ArrayList to String use below method
 String pipeSeparated = StringUtils.collectionToDelimitedString(language, "|");
 
 System.out.println("PIPE delimited String from ArrayList : " + pipeSeparated);
 
 //Let's generate colon delimited String from ArrayList
 
 String colonSeparated = StringUtils.collectionToDelimitedString(language, ":");
 System.out.println("Colon separated String from ArrayList : " + colonSeparated);
 }
}

Output:


String converted from ArrayList : Java,C++,Scala
PIPE delimited String from ArrayList : Java|C++|Scala
Colon separated String from ArrayList : Java:C++:Scala

That's all on How to convert ArrayList to String in Java. We have seen in this Java program, how Spring's utility method assist in converting ArrayList to delimited String in Java. If you don't want to use Spring API and want to convert ArrayList to String, you can use advanced for-each loop to create String from ArrayList as shown in below example :

/**
 *
 * Java program to convert ArrayList to String in Java using for-each loop
 * @author
 */
public class ArrayListTest {

 
 public static void main(String args[]) {

 // ArrayList to be converted into String
 ArrayList<String> language = new ArrayList<String>();
        language.add("HashSet");
        language.add("LinkedList");
        language.add("Vector");
 
 // Converting ArrayList to String in Java using advanced for-each loop
 StringBuilder sb = new StringBuilder();
 for(String str : language){
            sb.append(str).append(";"); //separating contents using semi colon
 }
 
 String strfromArrayList = sb.toString();
 System.out.println("String created from ArrayList in Java using for-each loop : " + strfromArrayList);
 }
}

Output:

String created from ArrayList in Java using for-each loop : HashSet;
LinkedList;
Vector;

If you notice output, you will see delimiter semicolon at the end also, which is unnecessary. you can remove that using String's lastIndexOf() method or you can use traditional for loop and not append delimiter after last element. Having said that, I prefer Spring framework to deal with such common things. If possible always use library method like Spring to convert ArrayList to String in Java.


Source: java67


The Tech Platform

0 comments

Komentar


bottom of page