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!
Getting Started with Svelte
Getting Started with Express
How to Serve Static Files with Nginx and Docker
How to Set Up Cron Jobs in Linux
How to deploy a .NET app using Docker
Best Visual Studio Code Extensions for 2022
How to deploy an Express app using Docker
How to deploy a Node app using Docker
Learn how to use v-model with a custom Vue component
Using Puppeteer and Jest for End-to-End Testing
Getting Started with Moment.js
Getting Started with React
