How to join values and include prefix and suffix with StringJoiner in Java



StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.
In this example we use values and want to join them into one String value separated with comma. We also want to include a prefix and a suffix in that String in form of brackets:

The output from the above code will be:

Now, since the add method of StringJoiner returns itself, a more fluent way of writing would be:







Search for more Java info on this site here:
Custom Search


Please type any questions here.