How To Convert Collection To String Inwards Coffee - Saltation Framework Example
Sunday, August 5, 2018
Add Comment
How to convert Collection to String inwards Java
Many times nosotros demand to convert whatever Collection similar Set or List into String like comma separated or whatever other delimiter delimited String. Though this is quite a footling chore for a Java programmer every bit you lot merely demand to Iterate through the loop in addition to practice a big String where private String are separated yesteryear a delimiter, you lot withal demand to grip cases similar the lastly chemical factor should non accept delimiter or at a bare minimum you lot demand to attempt out that code. I similar Joshua Bloch advice on Effective Java to purpose libraries for those mutual tasks allow it live an internal proprietary library or whatever opened upwards rootage library every bit used inwards previous examples of Spring, Apache Commons or Google’s Guava but signal is that you lot should avoid converting ArrayList to String similar mutual task yesteryear yourself on application code.
Collection to String Example inwards Java
I am a big fan of Spring framework in addition to nosotros purpose Spring inwards nigh of our projects in addition to nosotros accept already discussed few examples of Spring framework inwards Java earlier e.g. Using Spring to calculate execution time in addition to Spring safety to command Concurrent Sessions inwards Java. In this Java tutorial, I volition portion you lot an instance of converting List to delimited String, Set to delimit String or whatever other Collection flat into delimited String yesteryear using Spring framework's StringUtils class.
Spring provides 2 convenient method collectionToCommaDelimitedString in addition to collectionToDelimitedString which tin live used here. This instance is full general in addition to you lot tin convert whatever Collection into a comma separated or whatever delimiter separated String yesteryear 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 programme shows How to convert a List into a comma, colon in addition to pipage separated String.
Spring provides 2 convenient method collectionToCommaDelimitedString in addition to collectionToDelimitedString which tin live used here. This instance is full general in addition to you lot tin convert whatever Collection into a comma separated or whatever delimiter separated String yesteryear 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 programme shows How to convert a List into a comma, colon in addition to 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 programme 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 programme to demonstrate How to convert List or Set into String inwards Java.
* Spring framework's StringUtils flat furnish methods similar
* collectionToCommaDelimitedString in addition to collectionToDelimitedString
* which tin convert whatever Java collection flat 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 in addition to collectionToDelimitedString
* which tin convert whatever Java collection flat 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 accept seen How to convert List into a comma separated, colon dissever in addition to finally, PIPE separated String. You tin purpose whatever delimiter of your selection to impress all Collection elements every bit String inwards Java.
Further Reading
Spring Framework 5: Beginner to Guru
Expert Spring MVC in addition to Web Flow (read here)
Spring Fundamentals By Bryan Hansen (see here)
Spring Framework 5: Beginner to Guru
Expert Spring MVC in addition to 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 educate RESTful Web Service using Spring MVC inwards depth, I advise you lot bring together the REST amongst Spring certification class yesteryear Eugen Paraschiv. One of the best course of didactics to acquire REST amongst Spring MVC.
Sumber https://javarevisited.blogspot.com/
0 Response to "How To Convert Collection To String Inwards Coffee - Saltation Framework Example"
Post a Comment