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

Output

2. String.join

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

Output

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

Output

Java 8 Join String examples

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

References
Stream – Collectors.joining JavaDoc
StringJoiner JavaDoc

Download the complete source code, click link below

JoinStringExample.zip (4 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons