Since Java 8:
String.join(",", slist);
From Apache Commons library:
import org.apache.commons.lang3.StringUtils
Use:
StringUtils.join(slist, ',');
Another similar question and answer here
Answer from BobTheBuilder on Stack Overflow Top answer 1 of 3
508
Since Java 8:
String.join(",", slist);
From Apache Commons library:
import org.apache.commons.lang3.StringUtils
Use:
StringUtils.join(slist, ',');
Another similar question and answer here
2 of 3
10
You could count the total length of the string first, and pass it to the StringBuilder constructor. And you do not need to convert the Set first.
Set<String> abc = new HashSet<String>();
abc.add("A");
abc.add("B");
abc.add("C");
String separator = ", ";
int total = abc.size() * separator.length();
for (String s : abc) {
total += s.length();
}
StringBuilder sb = new StringBuilder(total);
for (String s : abc) {
sb.append(separator).append(s);
}
String result = sb.substring(separator.length()); // remove leading separator
java - Convert List<String> to delimited String - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... Closed 14 years ago. Possible Duplicate: Java: convert List to a join()d string More on stackoverflow.com
Java- Converting a string of numbers separated by spaces into an array?
You have the right idea. String.split() will return an array of strings. Then, if you want to treat any individual string in that array as an integer you can use Integer.parseInt() . If you want to create an array of integers, just make the array that way. More on reddit.com
how do I split a string into a list using multiple separators
You can use regular expression splitting, re.split: https://pythonexamples.org/python-split-string-by-regex/ More on reddit.com
How to test a "\n" new line with junit 4
show us the entire assertEquals() command. What are you comparing. If you compare strings use the .equals() method like so: assert("\n".equals(someStringVariable)); or for assertEquals it is essentially calling .equals() on the two provided arguments. More on reddit.com
Baeldung
baeldung.com › home › java › java string › convert a list to a comma-separated string
Convert a List to a Comma-Separated String | Baeldung
July 2, 2025 - You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence. Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time. ... List conversion is still a hot topic as it’s something that we do very often as Java developers. In this tutorial, we’ll learn how to convert a List of String to a comma-separated String using four different approaches.
Java Code Geeks
javacodegeeks.com › home › core java
Java 8 - Converting a List to String with Examples - Java Code Geeks
July 5, 2021 - First, we will understand how to make List to String using toString() method. Next, Collection to String with comma separator or custom delimiter using Java 8 Streams Collectors api and String.join() method.
Reverse Coding
reversecoding.net › java-8-convert-list-string-comma
Java 8 - Convert List to String comma separated | Reverse Coding
May 22, 2016 - For the old times’ sake, let’s have a look at Java 7 implementation. private static final String SEPARATOR = ","; public static void main(String[] args) { List<String> cities = Arrays.asList( "Milan", "London", "New York", "San Francisco"); StringBuilder csvBuilder = new StringBuilder(); for(String city : cities){ csvBuilder.append(city); csvBuilder.append(SEPARATOR); } String csv = csvBuilder.toString(); System.out.println(csv); //OUTPUT: Milan,London,New York,San Francisco, //Remove last comma csv = csv.substring(0, csv.length() - SEPARATOR.length()); System.out.println(csv); //OUTPUT: Milan,London,New York,San Francisco
TutorialsPoint
tutorialspoint.com › article › java-program-to-convert-a-list-of-string-to-comma-separated-string
Java program to convert a list of string to comma separated string
August 26, 2025 - In Java 8 and later, we can use the method Collectors.joining() from the Streams API to convert a list of strings to a comma-separated string.
Codemia
codemia.io › home › knowledge hub › best way to convert list to comma separated string in java
Best way to convert list to comma separated string in java | Codemia
July 31, 2025 - This is concise, readable, and directly expresses the intent: join these strings with this delimiter. If the list contains integers, domain objects, or values that need custom formatting, use a stream and map each element to a string first. ... 1import java.util.List; 2import java.util.stream.Collectors; 3 4public class Main { 5 public static void main(String[] args) { 6 List<Integer> numbers = List.of(1, 2, 3, 4); 7 8 String result = numbers.stream() 9 .map(String::valueOf) 10 .collect(Collectors.joining(", ")); 11 12 System.out.println(result); 13 } 14}
GeeksforGeeks
geeksforgeeks.org › java › convert-arraylist-to-comma-separated-string-in-java
Convert ArrayList to Comma Separated String in Java - GeeksforGeeks
July 23, 2025 - We can convert ArrayList to a comma-separated String using StringJoiner which is a class in java.util package which is used to construct a sequence of characters(strings) separated by a delimiter and optionally starting with a supplied prefix ...
javathinking
javathinking.com › blog › java-8-convert-list-to-string-with-separator
Java 8: Convert List to String with Separator — javathinking.com
Converting the list to a string with a separator allows you to present the data in a more organized way. When generating SQL queries dynamically, you may need to include a list of values in the IN clause. Converting the list to a comma-separated string can simplify the query construction process. import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ListToStringExample { public static void main(String[] args) { // Create a list of strings List<String> stringList = Arrays.asList("apple", "banana", "cherry"); // Convert the list to a string with a comma separator String result = stringList.stream() .collect(Collectors.joining(", ")); // Print the result System.out.println(result); } }
amitph
amitph.com › home › java › convert list to concatenated string with delimiter in java
List of Strings to Concatenated String with Delimiter in Java | amitph
November 22, 2024 - String concatenatedString = ""; String delimiter = " "; for (String word : words) { concatenatedString += concatenatedString.equals("") ? word : delimiter + word; } System.out.println(concatenatedString);Code language: Java (java) And the output: Convert List of Strings To String · However, you should not use this approach with the Strings. Because, Strings are immutable. Hence, every time you concat a String a separate String object is created.
ZetCode
zetcode.com › java › list2string
Java List to String - Converting a List to a String in Java
May 31, 2025 - This converts a list of integers into a comma-separated string. ... This tutorial has demonstrated various methods to convert a List to a string in Java. My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since ...
Medium
medium.com › @alxkm › converting-a-list-of-strings-to-a-comma-separated-string-in-java-multiple-approaches-ebb86745c889
Java Interview: Converting a List of Strings to a Comma-Separated String in Java: Multiple Approaches | by Alex Klimenko | Medium
August 9, 2025 - import com.google.common.base.Joiner; String result = Joiner.on(",").join(names); System.out.println(result); // Output: Alice,Bob,Charlie · Elegant API. Can skip nulls: Joiner.on(",").skipNulls().join(list); Requires external dependency. ... For most modern Java projects, String.join() and Collectors.joining() are the best and most concise options.
Kokaruk
kokaruk.com › 2017 › 09 › 15 › quickly-convert-list-of-ints-to-string-using-streams
Convert a List of Integers to a comma separated string · class Rock implements Throwable
August 23, 2020 - Originally published on 2017-09-14 Java List interface doesn’t have a method to join all members to coma separated string. Previous versions required iterating over the list and appending and instance of Stringbuilder. Using Java 8 Streams makes it more neat. https://gist.github.com/rmit-s3668804-Dzmitry-Kakaruk/7cb0f29c147cfb99e503f504ba3787f5
Top answer 1 of 2
40
StringBuilder sb = new StringBuilder();
for(String s: elementnames) {
sb.append(s).append(',');
}
sb.deleteCharAt(sb.length()-1); //delete last comma
String newString = sb.toString();
Update: Starting java 8, you can obtain the same results using:
List<String> elementNames = Arrays.asList("1", "2", "3");
StringJoiner joiner = new StringJoiner(",", "", "");
elementNames.forEach(joiner::add);
System.out.println(joiner.toString());
2 of 2
29
If you don't mind using the StringUtils library provided by apache, you could do:
// Output is "a,b,c"
StringUtils.join(["a", "b", "c"], ',');
https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html