How to convert Collection to String inwards Java
Many times nosotros demand to convert whatsoever Collection similar Set or List into String like comma separated or whatsoever other delimiter delimited String. Though this is quite a picayune task for a Java programmer equally you lot but demand to Iterate through the loop together with practise a big String where private String are separated past times a delimiter, you lot yet demand to handgrip cases similar the lastly chemical subdivision should non convey delimiter or at a bare minimum you lot demand to attempt that code. I similar Joshua Bloch advice on Effective Java to role libraries for those mutual tasks permit it live an internal proprietary library or whatsoever opened upwards root library equally used inwards previous examples of Spring, Apache Commons or Google’s Guava but indicate is that you lot should avoid converting ArrayList to String similar mutual task past times yourself on application code.
Collection to String Example inwards Java
I am a big fan of Spring framework together with nosotros role Spring inwards close of our projects together with nosotros convey already discussed few examples of Spring framework inwards Java earlier e.g. Using Spring to calculate execution time together with Spring safety to command Concurrent Sessions inwards Java. In this Java tutorial, I volition part you lot an illustration of converting List to delimited String, Set to delimit String or whatsoever other Collection shape into delimited String past times using Spring framework's StringUtils class.
Spring provides 2 convenient method collectionToCommaDelimitedString together with collectionToDelimitedString which tin live used here. This illustration is full general together with you lot tin convert whatsoever Collection into a comma separated or whatsoever delimiter separated String past times using this technique. Order of private String in delimited String is the guild on which they are stored inwards Collection e.g. List or Set. This detail Java plan shows How to convert a List into a comma, colon together with pipage separated String.
Spring provides 2 convenient method collectionToCommaDelimitedString together with collectionToDelimitedString which tin live used here. This illustration is full general together with you lot tin convert whatsoever Collection into a comma separated or whatsoever delimiter separated String past times using this technique. Order of private String in delimited String is the guild on which they are stored inwards Collection e.g. List or Set. This detail Java plan shows How to convert a List into a comma, colon together with pipage separated String.
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import org.springframework.util.StringUtils;
/**
* Simple Java plan to demonstrate How to convert List or Set into String inwards Java.
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import org.springframework.util.StringUtils;
/**
* Simple Java plan to demonstrate How to convert List or Set into String inwards Java.
* Spring framework's StringUtils shape supply methods similar
* collectionToCommaDelimitedString together with collectionToDelimitedString
* which tin convert whatsoever Java collection shape similar ArrayList or HashSet
* into comma delimited String.
*
* @author Javin
*/
public class CollectionToString{
public static void main(String args[]) {
//List amongst multiple Strings for testing
List<String> frameworks = Arrays.asList("Spring MVC", "Struts 2.0", "Velocity", "Wicket");
//let's convert this listing into comma separated String
String commaDelimitedString = StringUtils.collectionToCommaDelimitedString(frameworks);
System.out.println(commaDelimitedString);
//list to colon delimited String
String colonDelimitedString = StringUtils.collectionToDelimitedString(frameworks, ":");
System.out.println(colonDelimitedString);
//List to pipage delimited String
String pipeDelimitedString = StringUtils.collectionToDelimitedString(frameworks, "|");
System.out.println(pipeDelimitedString);
//Now let's convert Set into String inwards Java
HashSet<String> frameworkSet = new HashSet(frameworks);
//HashSet to comma separated String
commaDelimitedString = StringUtils.collectionToCommaDelimitedString(frameworkSet);
System.out.println(commaDelimitedString);
//Set to delimiter separated String using Spring
colonDelimitedString = StringUtils.collectionToDelimitedString(frameworkSet, ":");
System.out.println(colonDelimitedString);
//Set to pipage delimited String using Spring framework StringUtils
pipeDelimitedString = StringUtils.collectionToDelimitedString(frameworkSet, "|");
System.out.println(pipeDelimitedString);
}
}
Output
Spring MVC,Struts 2.0,Velocity,Wicket
Spring MVC:Struts 2.0:Velocity:Wicket
Spring MVC|Struts 2.0|Velocity|Wicket
Struts 2.0,Spring MVC,Velocity,Wicket
Struts 2.0:Spring MVC:Velocity:Wicket
Struts 2.0|Spring MVC|Velocity|Wicket
* collectionToCommaDelimitedString together with collectionToDelimitedString
* which tin convert whatsoever Java collection shape similar ArrayList or HashSet
* into comma delimited String.
*
* @author Javin
*/
public class CollectionToString{
public static void main(String args[]) {
//List amongst multiple Strings for testing
List<String> frameworks = Arrays.asList("Spring MVC", "Struts 2.0", "Velocity", "Wicket");
//let's convert this listing into comma separated String
String commaDelimitedString = StringUtils.collectionToCommaDelimitedString(frameworks);
System.out.println(commaDelimitedString);
//list to colon delimited String
String colonDelimitedString = StringUtils.collectionToDelimitedString(frameworks, ":");
System.out.println(colonDelimitedString);
//List to pipage delimited String
String pipeDelimitedString = StringUtils.collectionToDelimitedString(frameworks, "|");
System.out.println(pipeDelimitedString);
//Now let's convert Set into String inwards Java
HashSet<String> frameworkSet = new HashSet(frameworks);
//HashSet to comma separated String
commaDelimitedString = StringUtils.collectionToCommaDelimitedString(frameworkSet);
System.out.println(commaDelimitedString);
//Set to delimiter separated String using Spring
colonDelimitedString = StringUtils.collectionToDelimitedString(frameworkSet, ":");
System.out.println(colonDelimitedString);
//Set to pipage delimited String using Spring framework StringUtils
pipeDelimitedString = StringUtils.collectionToDelimitedString(frameworkSet, "|");
System.out.println(pipeDelimitedString);
}
}
Output
Spring MVC,Struts 2.0,Velocity,Wicket
Spring MVC:Struts 2.0:Velocity:Wicket
Spring MVC|Struts 2.0|Velocity|Wicket
Struts 2.0,Spring MVC,Velocity,Wicket
Struts 2.0:Spring MVC:Velocity:Wicket
Struts 2.0|Spring MVC|Velocity|Wicket
That’s all on How to convert Collection into Comma or delimiter separated String inwards Java. In this Java tutorial nosotros convey seen How to convert List into a comma separated, colon split upwards together with finally, PIPE separated String. You tin role whatsoever delimiter of your choice to impress all Collection elements equally String inwards Java.
Further Reading
Spring Framework 5: Beginner to Guru
Expert Spring MVC together with Web Flow (read here)
Spring Fundamentals By Bryan Hansen (see here)
Spring Framework 5: Beginner to Guru
Expert Spring MVC together with Web Flow (read here)
Spring Fundamentals By Bryan Hansen (see here)
Other Java Collection tutorials from Blog
P.S. - If you lot desire to acquire how to railroad train RESTful Web Service using Spring MVC inwards depth, I advise you lot bring together the REST amongst Spring certification class past times Eugen Paraschiv. One of the best course of report to acquire REST amongst Spring MVC.