Master Java’s StringJoiner: Clean, Efficient String Concatenation
This article explains how Java 8’s StringJoiner simplifies concatenating delimited strings compared to StringBuilder/StringBuffer, covering basic usage, constructors, methods, prefix/suffix handling, empty value configuration, stream‑style chaining, and the related String.join() API with practical code examples.
Basic Usage
StringJoiner is designed for concatenating strings with a delimiter. For example, joining "hello", "guys" and "欢迎使用StringJoiner" with a comma.
hello,guys,欢迎使用StringJoiner
Before Java 8, this required verbose StringBuilder code:
<code>StringBuilder sb = new StringBuilder();
sb.append("hello");
sb.append(",");
sb.append("guys");
sb.append(",");
sb.append("欢迎使用StringJoiner");
String str = sb.toString();</code>Using StringJoiner makes the code cleaner:
<code>public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",");
stringJoiner.add("hello");
stringJoiner.add("guys");
stringJoiner.add("欢迎使用StringJoiner");
System.out.println(stringJoiner.toString());
}</code>hello,guys,欢迎使用StringJoiner
Detailed Introduction
Class diagram of StringJoiner:
Key fields:
prefix – string placed before the result
delimiter – separator between elements
suffix – string placed after the result
value – the concatenated string
emptyValue – value returned when no elements are added
Constructors:
Two constructors are provided: one with only a delimiter, and another with delimiter, prefix, and suffix. The
emptyValuedefaults to prefix + suffix.
Stream‑style API
The
addmethod returns the StringJoiner itself, enabling fluent chaining similar to StringBuilder.
<code>public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",")
.add("hello")
.add("guys")
.add("欢迎使用StringJoiner");
System.out.println(stringJoiner.toString());
}</code>Prefix and Suffix Concatenation
Specify prefix and suffix in the constructor:
<code>public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",", "[", "]");
stringJoiner.add("hello");
stringJoiner.add("guys");
stringJoiner.add("欢迎使用StringJoiner");
System.out.println(stringJoiner.toString());
}</code>[hello,guys,欢迎使用StringJoiner]
Empty Value Handling
When no elements are added, the result depends on the empty value configuration.
<code>public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",");
System.out.println(stringJoiner.toString()); // prints empty string
}</code>With prefix and suffix:
<code>public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",", "[", "]");
System.out.println(stringJoiner.toString()); // prints []
}</code>Custom empty value via
setEmptyValue:
<code>public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",", "[", "]");
stringJoiner.setEmptyValue("void");
System.out.println(stringJoiner.toString()); // prints void
}</code>void
String.join()
Java 8 also provides
String.join(), a thin wrapper around StringJoiner for simple concatenation without prefix/suffix or empty‑value handling.
java.lang.String#join(CharSequence, CharSequence...)
java.lang.String#join(CharSequence, Iterable extends CharSequence )
Source code illustration:
Example usage:
<code>public static void main(String[] args) {
String str = String.join(",", "hello", "guys", "欢迎使用StringJoiner");
System.out.println(str);
}</code>hello,guys,欢迎使用StringJoiner
Summary
The article compares StringJoiner, StringBuilder, and String.join(), showing when to prefer each API. Use StringJoiner for repeated delimiter concatenation with optional prefix/suffix and empty‑value handling; use String.join() for simple cases.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.