![]() |
VOOZH | about |
The Collectors.joining() method in Java 8 Streams is a terminal operation used to combine stream elements into a single String. It works on streams of CharSequence and supports optional delimiters, prefixes, and suffixes, making it an efficient and beginner-friendly way to format output.
Java provides three variants of the Collectors.joining() method.
This is the simplest form of the joining() method. It does not take any parameters and concatenates the stream elements directly in the order of appearance.
public static Collector<CharSequence, ?, String> joining()
Example 1: Using joining() with a Character Array
GeeksforGeeks
Explanation:
Example 2: Using joining() with a List of Characters
GeeksforGeeks
Explanation
Example 3: Using joining() with a List of Strings
GeeksforGeeks
Explanation
This overloaded version allows specifying a delimiter that separates each element in the resulting string.
public static Collector<CharSequence, ?, String>
joining(CharSequence delimiter)
Example 1: Using joining(delimiter) with Characters
G, e, e, k, s, f, o, r, G, e, e, k, s
Explanation:
Example 2: Using joining(delimiter) with Strings
Geeks, for, Geeks
Explanation:
This version allows adding:
public static Collector<CharSequence, ?, String>
joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
Example 1: Using joining(delimiter, prefix, suffix) with Characters
[G, e, e, k, s, f, o, r, G, e, e, k, s]
Explanation:
Example 2: Using joining(delimiter, prefix, suffix) with Strings
{Geeks, for, Geeks}
Explanation: