joining() with No Arguments: Simple Concatenation
You'll learn how to use Collectors.joining() without any arguments to concatenate all stream elements into a single continuous string.
The Core Idea
When you call Collectors.joining() with no arguments, it takes every element in your stream and smashes them together into one long string—no spaces, no commas, no separators of any kind. It's the simplest form of string concatenation from a stream.
Think of it like stacking alphabet blocks in a line with no gaps between them. If you have blocks "H", "E", "L", "L", "O", they become "HELLO"—one continuous word.
How It Works
The joining() collector converts each stream element to a string (using toString() if needed) and concatenates them sequentially. Here's the basic syntax:
Stream<String> words = Stream.of("Java", "is", "powerful");
String result = words.collect(Collectors.joining());
// result = "Javaisowerful"
Notice there's nothing between the words—they're jammed together. This is perfect when you have single characters or when you genuinely want no separation:
Stream<String> letters = Stream.of("S", "t", "r", "e", "a", "m");
String word = letters.collect(Collectors.joining());
// word = "Stream"
When to Use It
This no-argument form is useful when:
- Concatenating individual characters into a word
- Building strings where elements are already formatted with their own separators
- Creating compact identifiers or codes without delimiters
For most human-readable output, you'll want separators (covered in future lessons), but this simple form has its place.
Key Takeaway: Collectors.joining() without arguments concatenates all stream elements directly into one continuous string with no separators between them.