StringJoiner Class in Java 8

In this tutorial, we will show you a StringJoiner Class example which is part of java.util package to join String’s. We will also discuss String join operation using + operator, StringBuffer and StringBuilder Class. Before moving forward know, Java 8 not only about Lambda Expressions and Streams. The String class in java 8 gets some useful updates. A new chars() method on String Class, using this we can build a Stream on that String.

We build a Stream on the letters of str1. It will print HELLO WORLD.

Now let see different way to perform concatenation Using + opearor then StringBuffer and finally suing StringBuilder class.

Example-1: Concatenation of String Using + operator

But some people will say we should not use concatenation with + sign, it should not be used as it is not efficient/ because multiple creation and deletion of intermediary strings. So people suggest use of StringBuffer class

Example-2: Concatenation of String Using StringBuffer

It’s better to user StringBuffer to concatenate Strings instead of using + sign to concatenate String, but StringBuffer is synchronized in Java. So it’s not best approach either to concatenate Strings in Java.

Example-3: Concatenation of String Using StringBuilder

In JDK5 onwards we have StringBuilder class.

It’s better to user StringBuilder to concatenate Strings instead of StringBuffer as this is not synchronized in Java, so it will be more efficient. But indeed JVM is smart enough, if this found StringBuffer is not called in multi-threaded environment than it will remove synchronized mechanism. So most of the time StringBuffer is as efficient as StringBuilder. In jDK7 the concatenation of String used with + sign already complied with StringBuilder. So you don’t need to use StringBuilder at all if you are using JDK7.

Example-5: Concatenation of String Using StringJoiner

But in Java 8 it even become much more simpler using new class StringJoiner. The StringJoiner joins the Strings with built in separators. So If I want to concatenate string using some separator I can do following.

This add method call can be chained as it returns the StringJoiner each time.

A StringJoiner can also built with a separator, prefix and suffix. The prefix will be added at the starting of produced String and suffix will be added at the end of produced String. This is very cool utility class to user. Let’s see it by an example.

In the above example we are taking a StringJoiner class overloaded constructor which takes a separator ( , ) as first input, than a prefix { , than finally it takes a suffix argument which is } in our case. The output of the above programme is:

The  nice thing is that StringJoiner can be used directly from the String class. We have new static join() method in String class which takes a separator as first argument and then varargs  with different elements which I want to concatenate.

Will print:

Instead of passing vararg, I can also pass an array instance or collection instance to this method.

Will print:



Leave a Reply

Your email address will not be published. Required fields are marked *