In this post, we’ll talk about the Collectors joining() method in Java.

1. Using Collectors.joining() method

The Collectors.joining() method returns a Collector that concatenates the input elements into a single string.

⮚ Convert a primitive character array to a string

Download  Run Code

⮚ Convert a list of Character to a string

Download  Run Code

 
This is equivalent to:

Download  Run Code

2. Using Collectors.joining(delimiter) method

Collectors.joining(delimiter) returns a Collector that concatenates the input elements, separated by the specified delimiter.

Download  Run Code

Output:

RED, BLUE, BLACK, GREEN

3. Using Collectors.joining(delimiter, prefix, suffix) method

Collectors.joining(delimiter, prefix, suffix) returns a Collector that concatenates the input elements, separated by the specified delimiter, with the specified prefix and suffix.

Download  Run Code

Output:

{RED, BLUE, BLACK, GREEN}

 
This is equivalent to:

Download  Run Code

Output:

{RED, BLUE, BLACK, GREEN}

That’s all about the Collectors class joining() method in Java.