How to Join a List of Strings with a Delimiter in Java
Table of Contents
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:
- The delimiter to use
- The list of strings to join
First let's start with a simple example list:
JAVAList<String> strings = Arrays.asList("one", "two", "three");
Now we can join the strings together with a delimiter, in this case, a comma:
JAVAList<String> strings = Arrays.asList("one", "two", "three");
String joined = String.join(", ", strings);
System.out.println(joined);
BASHone, 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:
JAVAList<String> strings = Arrays.asList("one", "two", "three");
String joined = strings.stream().collect(Collectors.joining(", "));
System.out.println(joined);
BASHone, 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.
JAVAList<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);
BASHone, 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:
JAVAList<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);
BASHone, 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!
- How to Install Node on Windows, macOS and Linux
- Git Tutorial: Learn how to use Version Control
- How to Serve Static Files with Nginx and Docker
- Best Visual Studio Code Extensions for 2022
- How to deploy a Node app using Docker
- How to Scrape the Web using Node.js and Puppeteer
- Getting Started with Handlebars.js
- Getting Started with Moment.js
- Using Push.js to Display Web Browser Notifications
- Building a Real-Time Note-Taking App with Vue and Firebase
- Setting Up Stylus CSS Preprocessor
- Setting Up a Local Web Server using Node.js