Java 8 - String.join() Example

I have been writing about new features of Java SE 8 since it's release in March 2014. Initially, my focus areas on much talked about lambda expressions and streams, but slowly I realize that Java 8 is not just about them. It has many more new features which will help Java developers in their day-to-day job as much lambdas and streams. One of them is the ability to join the String with any delimiter. It has added a class called StringJoiner in java.util package which we have seen earlier, but it has also added a new method on String class, the join() method, which finally allows you to join Strings in Java. You might have faced scenarios in past where you have a list of String or an array of String and you want to join them by a comma. Unfortunately, Java didn't have anything like Android's TextUtils.join() or JavaScript's Array.join() method which can join String on a delimiter.

Finally, the wait is over. JDK 8 has introduced a join() method which you can use to join any number of arbitrary String, an array of String or a list of String by a delimiter. Similar to String.split(), it is also a static method and you can call it as String.join(). It has two overloaded version, one accepts a delimiter and number of String as variable arguments and other accepts a delimiter and an Iterable to join elements from array and list.

In this article, I'll show you a couple of examples of how to use join() method of String class to join Strings in Java 8 and you can always look back to a good Java 8 book like Java SE 8 for Really Impatient to understand more about these little gems from Java 8.

How to use String.join() method

You can use the String.join() method to join a number of String literals or objects, String elements from an array, and String elements from List, Set or any collection because it accepts an iterable.

Here is an example of String.join() to join String literals by PIPE character

String banks = String.join("|", "Citibank", "Bank of America", "Chase");
System.out.println("banks: " + banks);

banks: Citibank|Bank of America|Chase
You can see that final String contains all three String literals separated by PIPE delimiter.

here is another example to join all elements of a List by commas

List<String> payCompanies = Arrays.asList("Apple pay", "Samsung Pay", "Paypal");
String wallats = String.join(",", payCompanies);
System.out.println("electronic wallats : " + wallats);

electronic wallets : Apple pay,Samsung Pay,Paypal

and here is the last example of String.join() to join elements of an array in Java:

String[] typesOfCards = {"Credit card", "Debit card", "Master Card"};
String cards = String.join(",", typesOfCards );
System.out.println("cards: " + cards);

cards: Credit card,Debit card,Master Card

Java 8 - String.join() Example

Java Program to join String

Here is our complete Java program to demonstrate other features of String.join() method. It's a static method similar to split, so you can directly call them on String literal or just pass the String objects you want to join. The program contains three examples, first to join arbitrary String literals, second to join elements from the list of String, and third to join elements from an array.

package test;

import java.util.Arrays;

 * Java Program to demonstrate how to use StringJoiner and String.join)( method
 * to join individual String and String form list and array.
public class Test {

  public static void main(String[] args) {

    // Joining arbitrary number of String
    String combined = String.join(" and ", "LinkedIn", "Microsoft");
    System.out.println("string joined by and : " + combined);

    // joining elements from a list of String
    String joined = String
        .join("|", Arrays.asList("Java", "Android", "Oracle"));
    System.out.println("String joined by pipe from list : " + joined);

    // joining String elements of an array
    String[] biggies = { "Apple", "Google", "Amazon" };
    String fromArray = String.join(",", biggies);
    System.out.println("String joined by comma from array: " + fromArray);



string joined by and : LinkedIn and Microsoft
String joined by pipe from list : Java|Android|Oracle
String joined by comma from array: Apple,Google,Amazon

That's all about how to use String.join() method in Java 8 to join String. Now, no need to use any third party library e.g. Google Guava or Apache commons, you can join String by just using JDK itself.

Other Java 8 tutorials you may like
If you come across any other detail about String.join() method, please let us know. It's one of the interesting methods which will be soon as popular as String.split() is at the moment.



  1. Looks like your payCompanies and typesOfCards output needs to be swapped.

    1. Indeed, thanks for pointing out, swapped now.