Java 8 StringJoiner

StringJoiner was introduced in Java 8. It joins String separated by a delimiter, starting with a supplied prefix and ending with a supplied suffix.

Let us understand it by looking into the below examples.

Join Strings using only delimiter

E2

Output:

E21

Join String with delimiter, prefix and suffix

E1

Output

O11

Merging two joiners

E32

Output

O3

Using Collectors.joining

E44

Output

O4

Using String.join

The static method join of String internally uses StringJoiner.

E5

O5

 

 

Leave a Reply

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