mscharhag, Programming and Stuff;

A blog about programming and software development topics, mostly focused on Java technologies including Java EE, Spring and Grails.

Friday, 21 March, 2014

Java can finally join strings

I am sure you were in a situation in which you wanted to join multiple strings. If you were using a programming language other than Java you probably used the join() function provided by the programming language. If you were using Java you could not do this. There was no join() method. The Java Standard Class Library provided you tools for building GUI applications, accessing databases, sending stuff over the network, doing XML transformations or calling remote methods. A simple method for joining a collection of strings was not included. For this you needed one of various third party libraries.

Luckily this time is over now! In Java 8 we finally can join Strings!

Java 8 added a new class called StringJoiner. As the name suggests we can use this class to join strings:

StringJoiner joiner = new StringJoiner(",");
String joined = joiner.toString(); // "foo,bar,baz"

// add() calls can be chained
joined = new StringJoiner("-")
 .toString(); // "foo-bar-baz"

StringJoiner is used internally by the two new static join() methods of String:

// join(CharSequence delimiter, CharSequence... elements)
String joined = String.join("/", "2014", "10", "28" ); // "2014/10/28"

// join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
List<String> list = Arrays.asList("foo", "bar", "baz");
joined = String.join(";", list); // "foo;bar;baz"

There is also a joining Collector available for the new Stream API:

List<Person> list = Arrays.asList(
 new Person("John", "Smith"),
 new Person("Anna", "Martinez"),
 new Person("Paul", "Watson ")

String joinedFirstNames =
 .collect(Collectors.joining(", ")); // "John, Anna, Paul"

So we do no longer need third party libraries for joining strings :-)


  • Siddharth Kulkarni - Sunday, 23 March, 2014

    What's the difference between StringJoiner and StringBuilder?

  • 宮川拓 - Sunday, 23 March, 2014

    With StringBuilder, you must append a delimiter in front of each element, except for the first one. StringJoiner takes a delimiter as a constructor argument and appends it automatically.

  • Earl - Thursday, 3 April, 2014

    Put another way, if I wrote a utility function to join strings using StringBuilder, would it be better to use StringJoiner (performance/efficiency wise) or would it be the same.

  • Mircea M - Monday, 20 October, 2014

    Thanks. I was looking for this.

  • Michael Fuhrmann - Thursday, 4 December, 2014

    private String join(String delimiter, String[] s)
    int ls = s.length;
    switch (ls)
    case 0: return "";
    case 1: return s[0];
    case 2: return s[0].concat(delimiter).concat(s[1]);
    int l1 = ls / 2;
    String[] s1 = Arrays.copyOfRange(s, 0, l1);
    String[] s2 = Arrays.copyOfRange(s, l1, ls);
    return join(delimiter, s1).concat(delimiter).concat(join(delimiter, s2));

    linear +: 5s
    linear .concat: 2.5s
    Java 8 .join: 3ms
    Divide&Conquer join: 7ms

  • Jon Earle - Thursday, 8 January, 2015

    Does not work with space as delimiter.

  • R - Wednesday, 26 April, 2017

    Why didn't they make the Joiner accept an iterator, an iterable (and not just of CharSequences) and an enumeration? I guess that would be too useful...

  • DK - Friday, 29 September, 2017

    Jon Earle, space seems to work fine as a delimiter.

Leave a reply