How to use String Joiner with Java 8

In this tutorial, JavaSampleApproach will show you ways to use StringJoiner with Java 8.

I. StringJoiner

StringJoiner is a Java approach to build a sequence of characters separated by a delimiter.
StringJoiner also supports optionally to start with a prefix & end with a suffix.

1. StringJoiner with a delimiter

Use constructor:
java.util.StringJoiner.StringJoiner(CharSequence delimiter)

2. StringJoiner with prefix & suffix

Use constructor:
java.util.StringJoiner.StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

II. String with static join function
1. Static Join Function with List Elements

Use: String java.lang.String.join(CharSequence delimiter, CharSequence... elements)

2. Static Join Function with an Iterable

Use: String java.lang.String.join(CharSequence delimiter, Iterable elements)

III. Java 8 Collectors.joining
1. Collectors.joining()

Use: Collector java.util.stream.Collectors.joining()

2. Collectors.joining with delimiter

Use: Collector java.util.stream.Collectors.joining(CharSequence delimiter)

3. Collectors.joining with prefix & suffix

Use:
Collector java.util.stream.Collectors.joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

IV. Sourcecode

Full SourceCode:

Run & Result:


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*