String.join

With Java 8 you can do this without any third party library.

If you want to join a Collection of Strings you can use the String.join() method:

List<String> list = Arrays.asList("foo", "bar", "baz");
String joined = String.join(" and ", list); // "foo and bar and baz"

Collectors.joining

If you have a Collection with another type than String you can use the Stream API with the joining Collector:

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

String joinedFirstNames = list.stream()
  .map(Person::getFirstName)
  .collect(Collectors.joining(", ")); // "John, Anna, Paul"

The StringJoiner class may also be useful.

Answer from micha on Stack Overflow
🌐
Medium
medium.com › @idiotN › javas-string-join-method-explained-9126d8b360c7
Java’s String.join() Method Explained
September 8, 2024 - Java’s String.join() Method Explained Java’s String.join() method is a convenient utility introduced in Java 8 that allows you to concatenate multiple strings with a specified delimiter. It is a …
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-join-method
Java String join() method
June 4, 2024 - The join() method of String class is introduced in Java 8. This method is used to concatenate strings with specified delimiter. It is also used to combine multiple elements of a collection with the specified separator.
🌐
DZone
dzone.com › data engineering › data › two ways to join string in java 8: stringjoiner and string.join examples
Two Ways to Join String in Java 8: StringJoiner and String.join Examples
May 15, 2019 - The JDK 8 API provides a couple of more ways to join Strings like you can join String by using the StringJoiner class or you can join the String by calling the String.join() method.
🌐
Blogger
javarevisited.blogspot.com › 2019 › 05 › 2-ways-to-join-string-in-java-8-examples-stringjoiner.html
StringJoiner.join() and String.join() Example in Java 8
June 25, 2025 - The JDK 8 API provides a couple of more ways to join Strings like you can join String by using the StringJoiner class or you can join the String by calling the String.join() method.
🌐
DEV Community
dev.to › satyam_gupta_0d1ff2152dcc › master-the-java-string-join-method-a-2025-guide-with-examples-best-practices-2oem
Master the Java String join() Method: A 2025 Guide with Examples & Best Practices - DEV Community
November 6, 2025 - In simple terms, String.join() is a static method added in Java 8 that acts as a string glue. You give it a delimiter (the glue) and some elements (the pieces), and it seamlessly sticks them all together into a single string, placing the delimiter ...
🌐
Baeldung
baeldung.com › home › java › java string › concatenating strings in java
Concatenating Strings in Java | Baeldung
May 8, 2025 - If our application is running on Java 8 or above, we can take advantage of the String.join method.
🌐
Java67
java67.com › 2016 › 08 › java-8-stringjoin-example.html
How to Join Multiple Strings in Java 8 - String join() Example | Java67
Similar to String.split(), it is also a static method and you can call it String.join(). It has two overloaded versions, one accepts a delimiter and number of String objects as variable arguments and the 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 the join() method of String class to join Strings in Java 8 and you can always look back to these free Java courses to understand more about these little gems from Java 8.
Find elsewhere
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java string.join()
Java String.join() with Examples - HowToDoInJava
February 7, 2023 - Java 8 has String.join() method where first parameter is separator and then you can pass either multiple strings or some instance of Iterable having instances of strings as second parameter. It will return the CSV in return.
🌐
Mkyong
mkyong.com › home › java8 › java 8 – stringjoiner example
Java 8 - StringJoiner example - Mkyong.com
February 9, 2016 - StringJoiner is used internally by static String.join(). ... List<String> list = Arrays.asList("java", "python", "nodejs", "ruby"); //java, python, nodejs, ruby String result = String.join(", ", list);
🌐
Blogger
javarevisited.blogspot.com › 2016 › 06 › 10-examples-of-joining-string-in-java-8.html
10 Examples of Joining String in Java - String.join vs StringJoiner
May 11, 2023 - Without wasting any more of your time, here are examples of using Stringjoiner class and the String.join() method to join multiple strings by comma, colon or any other separator in Java. The JDK 8 has added another text utility class called StringJoiner in java.util package.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-join-method-explained-77e1503095ec
Java’s String.join() Method Explained | Medium
September 2, 2024 - The String.join() method is one of the most useful utilities introduced in Java 8. It allows developers to combine a sequence of strings into a single string, with each element separated by a specified delimiter.
🌐
W3Schools
w3schools.com › java › ref_string_join.asp
Java String join() Method
The join() method joins one or more strings with a specified separator.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
StringJoiner vs String.join in Java 8 with Examples - Java Code Geeks
July 3, 2019 - The JDK 8 API provides a couple of more ways to join Strings like you can join String by using the StringJoiner class or you can join the String by calling the String.join() method.
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › join or append stream elements in java
Join or Append Stream Elements in Java
December 13, 2024 - Learn to join a stream of strings with a delimiter, prefix, and suffix using Collectors.joining(), String.join(), and append elements using StringBuilder. ... When working with a stream of strings, we may need to concatenate or append them into a single string. This Java 8 tutorial explores various techniques for joining or appending a stream of strings using a delimiter with examples using Java 8 Stream API and utilities such as String.join and Collectors.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string join() method
Java String join() Method
January 8, 2025 - This method takes a delimiter as a first parameter and an iterable as a second parameter whose elements must implement CharSequence and will be joined. public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
🌐
DZone
dzone.com › coding › java › concatenating strings in java 8
Concatenating Strings in Java 8
August 8, 2017 - People experienced with Java 8 Streams most probably know where we're going with this: the Collectors class: public static Collector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) { return new CollectorImpl<>( () -> new StringJoiner(delimiter, prefix, suffix), StringJoiner::add, StringJoiner::merge, StringJoiner::toString, CH_NOID); }
🌐
CompileBytes
compilebytes.com › home › examples › java examples › how to join a list into a string in java
How to Join a List Into a String in Java | CompileBytes
September 25, 2026 - Java 8 added three built-in tools: String.join for strings you already have, Collectors.joining for the end of a stream, and StringJoiner for building a result one piece at a time. This page covers all three, what they do with null elements, the trailing-separator bug in hand-written loops, the "a, b and c" variation, and String.repeat for repeating a string.
Top answer
1 of 1
54

First things first. This is not how you microbench Java

Read How do I write a correct micro-benchmark in Java? first. Your numbers are completely irrelevant, so lets ignore them.

Looking at the second example:

abc= "AZY"+"BAX"+"CBA"+...

These look like compile time constants to me. This String would be concatenated at compile time and there would be nothing to benchmark. This is a useless comparison as the whole point of the StringBuilder or String.join is to concatenate Strings that are not compile time constant.

Moving onto comparing the StringBuilder and String.join. Looking at the source code:

public static String join(CharSequence delimiter, CharSequence... elements) {
    Objects.requireNonNull(delimiter);
    Objects.requireNonNull(elements);
    // Number of elements not likely worth Arrays.stream overhead.
    StringJoiner joiner = new StringJoiner(delimiter);
    for (CharSequence cs: elements) {
        joiner.add(cs);
    }
    return joiner.toString();
}

This uses a StringJoiner. A StringJoiner simply uses a StringBuilder under the hood, so the two are equivalent.

It is often much more informative to look at the code than to try and benchmark performance. Even if you do benchmark correctly.

It's also worth noting that your first method, with join, joins the 1000 Strings on " " (space). Whereas your StringBuilder method simply appends them together. These two are not the same.

The point of the String.join method is that you can do:

String.join(", ", "a", "b", "c") // result is "a, b, c"

With a StringBuilder you would have to add a lot more code.