Java 8 Join String examples

Today, i want to show you some Java 8 Join String examples. I am sure you were in a situation in which you wanted to join multiple strings. Java 8 added a new class called StringJoiner. As the name suggests we can use this class to join strings. Beside, Java 8 also added the joining Collector for the new Stream API.

Let’s dig deeper via examples:

1. StringJoiner

Here is two examples: join string by a delimiter and join string with prefix and suffix


2. String.join

We’re using String.join() method to join a String


3. Collectors.joining

If you concern about joining objects to string, don’t worry just use the Collectors.joining method.

In this example, we are consume the POJO as below

Demo program


Java 8 Join String examples

That’s all about the Java 8 Join String examples.

Stream – Collectors.joining JavaDoc
StringJoiner JavaDoc

Download the complete source code, click link below (306 downloads)

Leave a Comment


Please share it if you found this useful
Hide Buttons