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!
- Support Us
Share Post Share
Getting Started with TypeScript
How to Install Node on Windows, macOS and Linux
Getting Started with Solid
Managing PHP Dependencies with Composer
Create an RSS Reader in Node
Getting Started with Electron
Git Tutorial: Learn how to use Version Control
Best Visual Studio Code Extensions for 2022
How to deploy a Deno app using Docker
Using Puppeteer and Jest for End-to-End Testing
Getting Started with React
Setting Up a Local Web Server using Node.js
