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:
StringBuilder sb = new StringBuilder();
sb.append("hello");
sb.append(",");
sb.append("guys");
sb.append(",");
sb.append("欢迎使用StringJoiner");
String str = sb.toString();Using StringJoiner makes the code cleaner:
public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",");
stringJoiner.add("hello");
stringJoiner.add("guys");
stringJoiner.add("欢迎使用StringJoiner");
System.out.println(stringJoiner.toString());
}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 emptyValue defaults to prefix + suffix.
Stream‑style API
The add method returns the StringJoiner itself, enabling fluent chaining similar to StringBuilder.
public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",")
.add("hello")
.add("guys")
.add("欢迎使用StringJoiner");
System.out.println(stringJoiner.toString());
}Prefix and Suffix Concatenation
Specify prefix and suffix in the constructor:
public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",", "[", "]");
stringJoiner.add("hello");
stringJoiner.add("guys");
stringJoiner.add("欢迎使用StringJoiner");
System.out.println(stringJoiner.toString());
}[hello,guys,欢迎使用StringJoiner]
Empty Value Handling
When no elements are added, the result depends on the empty value configuration.
public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",");
System.out.println(stringJoiner.toString()); // prints empty string
}With prefix and suffix:
public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",", "[", "]");
System.out.println(stringJoiner.toString()); // prints []
}Custom empty value via setEmptyValue:
public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",", "[", "]");
stringJoiner.setEmptyValue("void");
System.out.println(stringJoiner.toString()); // prints void
}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:
public static void main(String[] args) {
String str = String.join(",", "hello", "guys", "欢迎使用StringJoiner");
System.out.println(str);
}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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
