How to convert Collection to String in Java
Many times we need to convert any Collection like Set or List into String like comma-separated or any other delimiter delimited String. Though this is quite a trivial job for a Java programmer as you just need to Iterate through the loop and create a big String where individual String are separated by a delimiter, you still need to handle cases like the last element should not have a delimiter or at a bare minimum, you need to test that code.
I like Joshua Bloch advice on Effective Java to use libraries for those common tasks let it be an internal proprietary library or any open source library as used in previous examples of Spring, Apache Commons, or Google’s Guava but the point is that you should avoid converting ArrayList to String like common task by yourself on application code.
I like Joshua Bloch advice on Effective Java to use libraries for those common tasks let it be an internal proprietary library or any open source library as used in previous examples of Spring, Apache Commons, or Google’s Guava but the point is that you should avoid converting ArrayList to String like common task by yourself on application code.
Collection to String Example in Java
I am a big fan of the Spring framework and we use Spring in most of our projects and we have already discussed few examples of Spring framework in Java before e.g. Using Spring to calculate execution time and Spring security to control Concurrent Sessions in Java.
In this Java tutorial, I will share with you an example of converting List to delimited String, Set to delimit String, or any other Collection class into delimited String by using Spring framework's StringUtils class.
Spring provides two convenient method collectionToCommaDelimitedString and collectionToDelimitedString which can be used here. This example is general and you can convert any Collection into a comma-separated or any delimiter separated String by using this technique.
Order of individual String in delimited String is the order on which they are stored in Collection e.g. List or Set. This particular Java program shows How to convert a List into a comma, colon, and pipe separated String.
In this Java tutorial, I will share with you an example of converting List to delimited String, Set to delimit String, or any other Collection class into delimited String by using Spring framework's StringUtils class.
Spring provides two convenient method collectionToCommaDelimitedString and collectionToDelimitedString which can be used here. This example is general and you can convert any Collection into a comma-separated or any delimiter separated String by using this technique.
Order of individual String in delimited String is the order on which they are stored in Collection e.g. List or Set. This particular Java program shows How to convert a List into a comma, colon, and pipe 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 program to demonstrate How to convert List or Set into String in Java.
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import org.springframework.util.StringUtils;
/**
* Simple Java program to demonstrate How to convert List or Set into String in Java.
* Spring framework's StringUtils class provide methods like
* collectionToCommaDelimitedString and collectionToDelimitedString
* which can convert any Java collection class like ArrayList or HashSet
* into comma delimited String.
*
* @author Javin
*/
public class CollectionToString{
public static void main(String args[]) {
//List with multiple Strings for testing
List<String> frameworks = Arrays.asList("Spring MVC", "Struts 2.0", "Velocity", "Wicket");
//let's convert this list 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 pipe delimited String
String pipeDelimitedString = StringUtils.collectionToDelimitedString(frameworks, "|");
System.out.println(pipeDelimitedString);
//Now let's convert Set into String in 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 pipe 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 and collectionToDelimitedString
* which can convert any Java collection class like ArrayList or HashSet
* into comma delimited String.
*
* @author Javin
*/
public class CollectionToString{
public static void main(String args[]) {
//List with multiple Strings for testing
List<String> frameworks = Arrays.asList("Spring MVC", "Struts 2.0", "Velocity", "Wicket");
//let's convert this list 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 pipe delimited String
String pipeDelimitedString = StringUtils.collectionToDelimitedString(frameworks, "|");
System.out.println(pipeDelimitedString);
//Now let's convert Set into String in 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 pipe 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 in Java. In this Java tutorial, we have seen How to convert List into a comma-separated, colon separate, and finally, PIPE separated String. You can use any delimiter of your choice to print all Collection elements as String in Java.
Other Java Collection tutorials from Javarevisited Blog
P.S. - If you want to learn how to develop RESTful Web Service using Spring MVC in-depth, I suggest you join the REST with Spring certification class by Eugen Paraschiv. One of the best courses to learn REST with Spring MVC.
The Collection class in Java should be enhanced to simply have a join method
ReplyDeleteOther languages, such as Ruby, have way more rich APIs in classes like String and Array.
I think the Apache Common Lang lib can do this too. In fact. I think the spring one just either extends the Apache one or delegate it
ReplyDeletewhy don't you use toString method of that collection its comma separated list of set or list..
ReplyDeletenice article.
ReplyDeletethanks for sharing.
why don't you just use toString() instead??
ReplyDelete