Java StringJoiner Class: Simplify String Concatenation
Java 8 introduced a new utility class called StringJoiner
, which provides a convenient and expressive way to join strings together with a delimiter, prefix, and suffix. In this blog, we will explore the Java StringJoiner
class and its various methods, along with practical examples.
What is the StringJoiner class?
The StringJoiner
class is available in the java.util
package, simplifies the process of string concatenation by automatically handling the insertion of delimiters between elements. It allows you to specify a delimiter, as well as optional prefix and suffix strings. With StringJoiner
, you can easily join a collection of strings, such as an array or a list, into a single-string representation.
Basic usage of StringJoiner
Let’s start by looking at a basic example that demonstrates the usage of StringJoiner
:
package org.websparrow;
import java.util.StringJoiner;
public class StringJoinerExample {
public static void main(String[] args) {
StringJoiner joiner = new StringJoiner(", "); // Delimiter: comma
joiner.add("Atul");
joiner.add("Manish");
joiner.add("Rupendra");
String joinedString = joiner.toString();
System.out.println(joinedString);
}
}
In the above example, we create a StringJoiner
object with a comma as the delimiter. We then add three elements to the StringJoiner
using the add()
method. Finally, we obtain the joined string by calling the toString()
method on the StringJoiner
object.
The output of the above code will be:
Atul, Manish, Rupendra
Specifying Prefix and Suffix
Besides the delimiter, StringJoiner
allows you to specify optional prefix and suffix strings. Here’s an example that demonstrates the usage of prefix and suffix:
package org.websparrow;
import java.util.StringJoiner;
public class StringJoinerExample {
public static void main(String[] args) {
// Delimiter: comma, Prefix: [, Suffix: ]
StringJoiner joiner = new StringJoiner(", ", "[", "]");
joiner.add("Atul");
joiner.add("Manish");
joiner.add("Rupendra");
String joinedString = joiner.toString();
System.out.println(joinedString);
}
}
In this example, we create a StringJoiner
object with a comma as the delimiter, “[” as the prefix, and “]” as the suffix. The output will be:
[Atul, Manish, Rupendra]
P.S.
StringJoiner
is available from Java 8 onwards, so make sure you’re using a compatible Java version to utilize its functionality.
Handling Empty Values
StringJoiner
provides a useful method called setEmptyValue()
, which allows you to set a default value to return if there are no elements added to the StringJoiner
. This is especially handy when you want to handle the case of an empty collection differently.
Let’s see the below example:
package org.websparrow;
import java.util.StringJoiner;
public class StringJoinerExample {
public static void main(String[] args) {
// Delimiter: comma
StringJoiner joiner = new StringJoiner(", ");
// No elements added
joiner.setEmptyValue("No user found");
String joinedString = joiner.toString();
System.out.println(joinedString);
}
}
In this example, we set the default value to “No user found” using the setEmptyValue()
method. Since there are no elements added, the output will be:
No user found
Merging StringJoiners
Another useful feature of StringJoiner
is the ability to merge the contents of one StringJoiner
with another.
Let’s take a look at an example:
package org.websparrow;
import java.util.StringJoiner;
public class StringJoinerExample {
public static void main(String[] args) {
// Delimiter: comma
StringJoiner joiner1 = new StringJoiner(", ");
joiner1.add("Apple");
joiner1.add("Banana");
// Delimiter: hyphen
StringJoiner joiner2 = new StringJoiner("-");
joiner2.add("Orange");
joiner2.add("Mango");
joiner1.merge(joiner2);
String joinedString = joiner1.toString();
System.out.println(joinedString);
}
}
In the above example, we create two StringJoiner
objects with different delimiters. We add elements to each of them. Then, we merge joiner2
into joiner1
using the merge()
method.
The output will be:
Apple, Banana, Orange-Mango