How to Use Collectors.joining in Java: Simple Guide
Use
Collectors.joining() to combine strings from a stream into one string. You can join without a delimiter, or add a delimiter, prefix, and suffix by using its overloaded forms.Syntax
The Collectors.joining() method has three forms:
joining(): Joins strings without any delimiter.joining(CharSequence delimiter): Joins strings separated by the given delimiter.joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix): Joins strings with delimiter, and adds prefix and suffix around the result.
java
Collectors.joining() Collectors.joining("delimiter") Collectors.joining("delimiter", "prefix", "suffix")
Example
This example shows how to join a list of words into a single string using different Collectors.joining() methods.
java
import java.util.List; import java.util.stream.Collectors; public class JoiningExample { public static void main(String[] args) { List<String> words = List.of("apple", "banana", "cherry"); // Join without delimiter String joined1 = words.stream() .collect(Collectors.joining()); // Join with comma delimiter String joined2 = words.stream() .collect(Collectors.joining(", ")); // Join with delimiter, prefix, and suffix String joined3 = words.stream() .collect(Collectors.joining(", ", "[", "]")); System.out.println("Joined without delimiter: " + joined1); System.out.println("Joined with comma: " + joined2); System.out.println("Joined with prefix and suffix: " + joined3); } }
Output
Joined without delimiter: applebananacherry
Joined with comma: apple, banana, cherry
Joined with prefix and suffix: [apple, banana, cherry]
Common Pitfalls
Common mistakes when using Collectors.joining() include:
- Trying to join non-string elements without converting them to strings first.
- Forgetting to provide a delimiter when you want separation, resulting in concatenated words.
- Using
Collectors.joining()on an empty stream without handling the empty result.
Always ensure the stream elements are strings or converted to strings before joining.
java
import java.util.List; import java.util.stream.Collectors; public class JoiningPitfall { public static void main(String[] args) { List<Integer> numbers = List.of(1, 2, 3); // Wrong: This will cause a compile error because elements are not strings // String result = numbers.stream().collect(Collectors.joining()); // Right: Convert integers to strings first String result = numbers.stream() .map(String::valueOf) .collect(Collectors.joining(", ")); System.out.println(result); // Output: 1, 2, 3 } }
Output
1, 2, 3
Quick Reference
Summary tips for using Collectors.joining():
- Use
joining()for simple concatenation. - Use
joining(delimiter)to separate elements clearly. - Use
joining(delimiter, prefix, suffix)to add wrappers around the joined string. - Convert non-string elements to strings before joining.
- Handle empty streams to avoid unexpected empty strings.
Key Takeaways
Collectors.joining() combines stream strings into one string efficiently.
You can add delimiters, prefixes, and suffixes for formatted output.
Always convert non-string stream elements to strings before joining.
Without a delimiter, strings join directly without spaces or commas.
Handle empty streams to avoid empty or unexpected results.