Join a list of strings with '[' as prefix, ']' as suffix, and ',' as delimiter using streams.
Source: Dev.to
Steps
Convert the list to a stream
languageList.stream()Collect the elements into a single string using
Collectors.joiningwith a delimiter, a prefix, and a suffix:String result = languageList.stream() .collect(Collectors.joining(",", "[", "]"));Collectors.joining(",", "[", "]")joins the elements of the stream into a singleString, separated by commas, and adds[at the beginning and]at the end.
Result
The code above produces a string like:
[Java,Python,JavaScript]Visual Example
