In this post, we will see How to convert List to comma separated String in Java. We will cover different ways to convert List and Arrays to comma separated String.
Using org.apache.commons.lang3.StringUtils join() method.
package com.javatute.serviceimpl; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; public class StringUtilsJoin { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("james"); list.add("rod"); list.add("duke"); list.add("jack"); String s1 = StringUtils.join(list, ","); System.out.println("Using Apche Commons StringUtils -> " + s1); } }
Output is –
Using Apche Commons StringUtils -> james,rod,duke,jack
The join() method has different overloaded versions. Using that we can convert array to a comma separated String too. Let’s see an example.
package com.javatute.serviceimpl; import org.apache.commons.lang3.StringUtils; public class StringUtilsJoin { public static void main(String[] args) { String[] stringElements = new String[4]; stringElements[0] = "james"; stringElements[1] = "rod"; stringElements[2] = "duke"; stringElements[3] = "jack"; String s1 = StringUtils.join(stringElements, ","); System.out.println("Using Apche Commons StringUtils -> " + s1); } }
Output is –
Using Apche Commons StringUtils -> james,rod,duke,jack
Using Java 8 String.join() method.
In java 8 join() method has been introduced in String class which has two overloaded versions as below.
- public static String join(CharSequence delimiter, CharSequence… elements)
- public static String join(CharSequence delimiter, Iterable elements)
Converting List to comma separated String Using Java 8 join() method.
package com.javatute.serviceimpl; import java.util.ArrayList; import java.util.List; public class StringJoin { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("james"); list.add("rod"); list.add("duke"); list.add("jack"); String s2 = String.join(",", list); System.out.println("Using Java 8 String.join() -> " + s2); } }
Output is –
Using Java 8 String.join() -> james,rod,duke,jack
Converting Arrays to comma separated String Using Java 8 join() method.
package com.javatute.serviceimpl; import org.apache.commons.lang3.StringUtils; public class StringUtilsJoin { public static void main(String[] args) { String[] stringElements = new String[4]; stringElements[0] = "james"; stringElements[1] = "rod"; stringElements[2] = "duke"; stringElements[3] = "jack"; String s1 = StringUtils.join(stringElements, ","); System.out.println("Using Java 8 String.join() -> " + s1); } }
Output is –
Using Java 8 String.join() -> james,rod,duke,jack
We can also use Collectors.joining() method where we can pass delimiter.
public static Collector joining(CharSequence delimiter)
package com.javatute.serviceimpl; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class StringUtilsJoin { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("james"); list.add("rod"); list.add("duke"); list.add("jack"); String s1 = list.stream().collect(Collectors.joining(",")); System.out.println("Using Java 8 Collectors.joining() -> "+s1); } }
Output is –
Using Java 8 Collectors.joining() -> james,rod,duke,jack
Using org.springframework.util.StringUtils class.
Spring provides StringUtils util class, which contains collectionToCommaDelimitedString() method.
public static String collectionToCommaDelimitedString(Collection coll);
package com.javatute.serviceimpl; import java.util.ArrayList; import java.util.List; import org.springframework.util.StringUtils; public class StringUtilsJoin { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("james"); list.add("rod"); list.add("duke"); list.add("jack"); String s1 = StringUtils.collectionToCommaDelimitedString(list); System.out.println("Using Spring StringUtils.join() -> " + s1); } }
Output is –
Using Spring StringUtils.join() -> james,rod,duke,jack
We can also convert Arrays to comma separated String using arrayToCommaDelimitedString() method.
public static String arrayToCommaDelimitedString(Object[] arr)
package com.javatute.serviceimpl; import org.springframework.util.StringUtils; public class StringUtilsJoin { public static void main(String[] args) { String[] stringElements = new String[4]; stringElements[0] = "james"; stringElements[1] = "rod"; stringElements[2] = "duke"; stringElements[3] = "jack"; String s1 = StringUtils.arrayToCommaDelimitedString(stringElements); System.out.println("Using Spring StringUtils.join() -> " + s1); } }
Output is –
Using Spring StringUtils.join() -> james,rod,duke,jack
Using for-loop and StringBuilder.
package com.javatute.serviceimpl; import java.util.ArrayList; import java.util.List; public class ListToStringCommaSeparator { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("james"); list.add("rod"); list.add("duke"); list.add("jack"); StringBuilder sb1 = new StringBuilder(); for (String s : list) { sb1.append(s).append(","); } System.out.println("Using for loop and StringBuilder -> " + sb1); } }
Output is –
Using for loop and StringBuilder -> james,rod,duke,jack,
Converting an array to Comma separated string.
package com.javatute.serviceimpl; public class ListToStringCommaSeparator { public static void main(String[] args) { String[] stringElements = new String[4]; stringElements[0] = "james"; stringElements[1] = "rod"; stringElements[2] = "duke"; stringElements[3] = "jack"; StringBuilder sb1 = new StringBuilder(); for (String s : stringElements) { sb1.append(s).append(","); } sb1.deleteCharAt(sb1.length() - 1); System.out.println("Using for loop and StringBuilder -> " + sb1); } }
That’s all about How to convert List to comma separated String in Java.
You may like.
- CollectionUtils isEmpty() Example in Java.
- StringUtils isEmpty() and IsBlank() Example in Java.
- StringUtils join() Example in Java.
- ArrayList in Java With Example.
- How to define custom ArrayList in java.
- How get method of ArrayList work internally in java.
- ArrayList vs Vector in java.
- How ArrayList works internally in Java.
ArrayList docs.
Summary – We have seen How to convert List to comma separated String in Java using StringUtils join() and Spring StringUtils class.