Append quotes to strings in an array and make it comma separated string in Java

Java 8 has Collectors.joining() and its overloads. It also has String.join.
Using a Stream and a Collector

Function<String,String> addQuotes = s -> "\"" + s + "\"";

String result = listOfStrings.stream()
  .map(addQuotes)
  .collect(Collectors.joining(", "));
String result = listOfStrings.stream() .map(s -> "\"" + s + "\"") .collect(Collectors.joining(", "));
String result = listOfString.isEmpty() ? "" : "\"" + String.join("\", \"", listOfStrings) + "\"";

Happy Coding!


Also published on Medium.

Default image
Vignesh A Sathiyanantham
AWS Certified developer with 8 years of extensive experience working for Cloud and Datacenter solutions and leading a small team in a lean startup based on Bangalore, India
Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.