How To Convert Collection To String Inward Coffee - Jump Framework Example
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 simply demand to Iterate through the loop in addition to exercise a big String where private String are separated yesteryear a delimiter, you lot nevertheless demand to handgrip cases similar the lastly chemical factor should non convey delimiter or at a bare minimum you lot demand to evidence that code. I similar Joshua Bloch advice on Effective Java to role libraries for those mutual tasks allow 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 betoken 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 role Spring inwards nearly of our projects in addition to nosotros convey 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 part you lot an representative of converting List to delimited String, Set to delimit String or whatsoever other Collection shape into delimited String yesteryear using Spring framework's StringUtils class.
Spring provides ii convenient method collectionToCommaDelimitedString in addition to collectionToDelimitedString which tin live used here. This representative is full general in addition to you lot tin convert whatsoever Collection into a comma separated or whatsoever delimiter separated String yesteryear using this technique. Order of private String in delimited String is the club 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 in addition to piping separated String.
Spring provides ii convenient method collectionToCommaDelimitedString in addition to collectionToDelimitedString which tin live used here. This representative is full general in addition to you lot tin convert whatsoever Collection into a comma separated or whatsoever delimiter separated String yesteryear using this technique. Order of private String in delimited String is the club 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 in addition to piping 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 render methods similar
* collectionToCommaDelimitedString in addition to 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 piping 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 piping 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 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 piping 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 piping 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 dissever in addition to finally, PIPE separated String. You tin role whatsoever delimiter of your pick to impress all Collection elements equally 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 railroad train RESTful Web Service using Spring MVC inwards depth, I propose you lot bring together the REST amongst Spring certification class yesteryear Eugen Paraschiv. One of the best class to acquire REST amongst Spring MVC.
Belum ada Komentar untuk "How To Convert Collection To String Inward Coffee - Jump Framework Example"
Posting Komentar