How to convert a List of Strings to a Comma Separated String with Single Quotes In Java

You can convert a list of strings to a comma-separated string with single quotes around each element in Java by iterating through the list and constructing the desired string. Here's how you can do it:


This Java code uses Java Streams to map each element of the list to a string with single quotes around it using the map operation. Then, it collects these strings into a single comma-separated string using the Collectors.joining method.

The output of this code will be:

'apple', 'banana', 'orange'

Each element in the list is surrounded by single quotes and separated by commas, as desired.

No comments:

Post a Comment