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

Vignesh A Sathiyanantham
1 min readJul 8, 2019

--

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!

Originally published at asvignesh.

--

--

Vignesh A Sathiyanantham
Vignesh A Sathiyanantham

Written by Vignesh A Sathiyanantham

Decade of experience in building product related to private datacenter and public cloud #Java #AWS #VMware #Python

No responses yet