introduce
StringJoiner is a class in the java.util package that constructs an optional delimiter sequence of characters, starting with the provided prefix and ending with the provided suffix. While this can also be done with the help of the StringBuilder class by appending a delimiter after each string, StringJoiner provides an easy way to do this without writing a lot of code.
The StringJoiner class has two constructors and five public methods. The most common of these are the Add and toString methods, similar to the Append and toString methods in StringBuilder.
usage
StringJoiner is relatively simple to use. In this code, we use StringJoiner to concatenate strings.
public class StringJoinerTest {
public static void main(String[] args) {
StringJoiner sj = new StringJoiner("Hello");
sj.add("World");
sj.add("Java");
System.out.println(sj.toString());
StringJoiner sj1 = new StringJoiner(":"."["."]");
sj1.add("Hello").add("World").add("Java"); System.out.println(sj1.toString()); }}Copy the code
Output from the above code:
WorldHelloJava
[Hello:World:Java]
Copy the code
Note that when StringJoiner(CharSequence Delimiter) initializes a StringJoiner, delimiter is actually a delimiter, not the initial value of the mutable string.
StringJoiner(CharSequence delimiter,CharSequence prefix,CharSequence suffix) The second and third parameters are the prefix and suffix of the concatenated string respectively.
The principle of
Now that we’ve introduced the simple usage, let’s take a look at how StringJoiner works and how it’s implemented. Let’s focus on the add method:
public StringJoiner add(CharSequence newElement) {
prepareBuilder().append(newElement);
return this;
}
private StringBuilder prepareBuilder(a) {
if(value ! =null) {
value.append(delimiter);
} else {
value = new StringBuilder().append(prefix);
}
return value;
}
Copy the code
StringBuilder. StringBuilder is what StringBuilder is all about
When we find out that StringJoiner is implemented using StringBuilder, we can probably guess that the performance loss should be similar to that of using StringBuilder directly!
Why StringJoiner
Now that we have learned how to use StringJoiner and how it works, we are probably wondering why Java 8 should define a StringBuilder when it already has one. What are the benefits?
If you know Java 8 well enough, you can probably guess that it has something to do with Stream.
The author also found the answer in the Java Doc:
A StringJoiner may be employed to create formatted output from a Stream using Collectors.joining(CharSequence)
Imagine, in Java, if we had a List like this:
List<String> list = ImmutableList.of("Hello"."World"."Java");
Copy the code
If we want to concatenate it into a string of the following form:
Hello,World,Java
Copy the code
To meet requirements like this, the StringJoiner provided in Java 8 comes in handy. All you need is one line of code:
List. The stream (). Collect (Collectors. Joining (” : “)). In the expression used above, the source code for Collectors. Joining is as follows:
public staticCollector<CharSequence, ? , String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) {return new CollectorImpl<>(
() -> new StringJoiner(delimiter, prefix, suffix),
StringJoiner::add, StringJoiner::merge,
StringJoiner::toString, CH_NOID);
}
Copy the code
And that’s what StringJoiner does.
Of course, perhaps using StringBuilder directly in a Collector would seem to achieve similar functionality, albeit with a little more hassle. So, Java 8 provides StringJoiner to enrich the use of Stream.
And StringJoiner can easily add prefixes and suffixes. For example, if we want the string to be [Hello,World,Java] instead of Hello,World,Java, StringJoiner’s advantage is even greater.
conclusion
This article introduced StringJoiner, the mutable string class provided in Java 8, which can be used for string concatenation.
StringJoiner is actually implemented using StringBuilder, so its performance is about the same as StringBuilder, and it’s not thread-safe.
If string concatenation is needed in daily development, how to select it?
1. For simple string concatenation, consider using “+”.
2. Consider Using StringBuilder and StringBuffer if you are concatenating strings in a for loop.
3. If you are concatenating strings using a List, consider Using StringJoiner.
conclusion
Welcome to wechat public account “Code zonE”, focusing on sharing Java, cloud computing related content, including SpringBoot, SpringCloud, microservices, Docker, Kubernetes, Python and other related technology dry goods, looking forward to meeting you!