Some fourth dimension nosotros require to convert ArrayList to String inwards Java programming linguistic communication inwards companionship to exceed that String to stored procedure, whatsoever method or whatsoever other program. Unfortunately Java collection framework doesn't render whatsoever at i time utility method to convert ArrayList to String inwards Java. But Spring framework which is famous for dependency Injection in addition to its IOC container too provides API alongside mutual utilities similar method to convert Collection to String inwards Java. You tin dismiss convert ArrayList to String using Spring Framework's StringUtils class. StringUtils shape render 3 methods to convert whatsoever collection e.g. ArrayList to String inwards Java, equally 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 higher upward method you lot tin dismiss convert ArrayList to comma separated String, pipe separated String or you lot tin dismiss render whatsoever other separator. You tin dismiss too banking company fit How to convert Collection to String for to a greater extent than examples of these methods.
package test;
import java.util.ArrayList;
import org.springframework.util.StringUtils;
/**
*
* Java plan to convert ArrayList to String inwards Java using Spring framework.
* It uses Spring's StringUtils shape to practice String from ArrayList inwards Java
* @author
*/
public class ArrayListProgram {
public static void main(String args[]) {
// ArrayList to live converted into String
ArrayList<String> linguistic communication = new ArrayList<String>();
language.add("Java");
language.add("C++");
language.add("Scala");
// Converting ArrayList to String using Spring API , resultant is comma separated String
String arraylistToString = StringUtils.collectionToCommaDelimitedString(language);
System.out.println("String converted from ArrayList : " + arraylistToString);
//If you lot desire to role separater other than coma piece converting ArrayList to String role 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
import java.util.ArrayList;
import org.springframework.util.StringUtils;
/**
*
* Java plan to convert ArrayList to String inwards Java using Spring framework.
* It uses Spring's StringUtils shape to practice String from ArrayList inwards Java
* @author
*/
public class ArrayListProgram {
public static void main(String args[]) {
// ArrayList to live converted into String
ArrayList<String> linguistic communication = new ArrayList<String>();
language.add("Java");
language.add("C++");
language.add("Scala");
// Converting ArrayList to String using Spring API , resultant is comma separated String
String arraylistToString = StringUtils.collectionToCommaDelimitedString(language);
System.out.println("String converted from ArrayList : " + arraylistToString);
//If you lot desire to role separater other than coma piece converting ArrayList to String role 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 inwards Java. We bring seen inwards this Java program, how Spring's utility method aid inwards converting ArrayList to delimited String inwards Java. If you lot don't desire to role Spring API in addition to desire to convert ArrayList to String, you lot tin dismiss role advanced for-each loop to practice String from ArrayList equally shown inwards below instance :
/**
*
* Java plan to convert ArrayList to String inwards Java using for-each loop
* @author
*/
public class ArrayListTest {
public static void main(String args[]) {
// ArrayList to live converted into String
ArrayList<String> linguistic communication = new ArrayList<String>();
language.add("HashSet");
language.add("LinkedList");
language.add("Vector");
// Converting ArrayList to String inwards 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 inwards Java using for-each loop : " + strfromArrayList);
}
}
Output:
String created from ArrayList inwards Java using for-each loop : HashSet;LinkedList;Vector;
*
* Java plan to convert ArrayList to String inwards Java using for-each loop
* @author
*/
public class ArrayListTest {
public static void main(String args[]) {
// ArrayList to live converted into String
ArrayList<String> linguistic communication = new ArrayList<String>();
language.add("HashSet");
language.add("LinkedList");
language.add("Vector");
// Converting ArrayList to String inwards 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 inwards Java using for-each loop : " + strfromArrayList);
}
}
Output:
String created from ArrayList inwards Java using for-each loop : HashSet;LinkedList;Vector;
If you lot honour output, you volition come across delimiter semicolon at the destination also, which is unnecessary. you lot tin dismiss take that using String's lastIndexOf() method or you lot tin dismiss role traditional for loop in addition to non append delimiter subsequently terminal element. Having said that, I prefer Spring framework to bargain alongside such mutual things. If possible ever role library method similar Spring to convert ArrayList to String inwards Java.
Further Learning
Java In-Depth: Become a Complete Java Engineer
How to convert String to Integer inwards Java
No comments:
Post a Comment