How to Join a List of Strings with a Delimiter in Java

Updated onbyAlan Morel
How to Join a List of Strings with a Delimiter in Java

In Java, lists are common because they allow you to store several pieces of the same type of data inside a single variable.

For example, you can store a list of string, a list of numbers, or a list of objects.

When you have a list of strings, you'll sometimes want to join them together into a single string.

In this post, we'll learn all the best ways to join a list of strings together with a delimiter in Java.

Using String.join()

The most straightforward way to join a list of strings together is to use the String.join() method.

This method is called on the String class and takes two arguments:

  1. The delimiter to use
  2. The list of strings to join

First let's start with a simple example list:

JAVA
List<String> strings = Arrays.asList("one", "two", "three");

Now we can join the strings together with a delimiter, in this case, a comma:

JAVA
List<String> strings = Arrays.asList("one", "two", "three"); String joined = String.join(", ", strings); System.out.println(joined);
BASH
one, two, three

As expected, each string is joined together with a comma and a space.

Join using the Stream API

A more modern way to join a list of strings together is to use the Stream API.

With this API, we can take a list, then turn it into a stream, and then use the collect() method to join the strings together with the joining() method.

Here's an example:

JAVA
List<String> strings = Arrays.asList("one", "two", "three"); String joined = strings.stream().collect(Collectors.joining(", ")); System.out.println(joined);
BASH
one, two, three

Join using StringJoiner

The StringJoiner class is a bit more flexible than the String.join() method. It allows you to specify a prefix and suffix for the joined string.

The class accepts a delimiter and an optional prefix and suffix. Since we don't need a prefix or suffix, we'll just pass in the delimiter.

JAVA
List<String> strings = Arrays.asList("one", "two", "three"); StringJoiner joiner = new StringJoiner(", "); for (String string : strings) { joiner.add(string); } String joined = joiner.toString(); System.out.println(joined);
BASH
one, two, three

Join using StringBuilder

Another way to join a list of strings together is to use a StringBuilder.

We can just loop through the list and append each string to the StringBuilder with the delimiter in between.

Here's an example:

JAVA
List<String> strings = Arrays.asList("one", "two", "three"); StringBuilder builder = new StringBuilder(); for (String string : strings) { builder.append(string).append(", "); } String joined = builder.toString(); System.out.println(joined);
BASH
one, two, three

Conclusion

In this post, we learned how to join a list of strings together with a delimiter in Java.

Our option included using the String.join() method, the Stream API, the StringJoiner class, and a StringBuilder.

Thanks for reading!

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.