Course contentsShow
← Java
Lesson 1559 of 2,87034. Collectors in DepthFree lesson

The Collectors.joining() Method Overview

Understanding how joining() collects stream elements into a single concatenated String, its three overloads, and basic usage.

The Collectors.joining() Method Overview

What you'll learn: How to concatenate stream elements into a single String using Collectors.joining() and its three different forms.

What Is joining()?

Collectors.joining() is a specialized collector that takes a stream of elements (typically Strings) and combines them into a single concatenated String. Think of it like stringing beads together—each stream element becomes part of one continuous chain.

The Three Overloads

The joining() method comes in three flavors, each offering different levels of control:

1. joining() — Simple Concatenation

Merges all elements directly with no separator:

Stream.of("Java", "Python", "C++").collect(Collectors.joining());
// Result: "JavaPythonC++"

2. joining(CharSequence delimiter) — With Separator

Inserts a delimiter between each element:

Stream.of("apple", "banana", "cherry").collect(Collectors.joining(", "));
// Result: "apple, banana, cherry"

3. joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) — Fully Decorated

Adds a delimiter between elements, plus prefix and suffix wrappers:

Stream.of("red", "green", "blue").collect(Collectors.joining(", ", "[", "]"));
// Result: "[red, green, blue]"

Why It Matters

Before streams, building formatted strings from collections required loops and StringBuilder management. The joining() collector handles all that boilerplate internally, making string construction from collections both elegant and efficient.

Key Takeaway: Collectors.joining() transforms stream elements into a single concatenated String, with three overloads providing control over delimiters, prefixes, and suffixes—eliminating manual StringBuilder loops.