Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

How to Convert a List of String to Comma Separated String (CSV) in Java 8? Example

Before Java 8, it was not straightforward to convert a list of String to a comma-separated String. You have to loop through the collection or list and join them manually using String concatenation, which can take up more than 4 lines of code. Of course, you could encapsulate that into your own utility method and you should but JDK didn't provide anything useful for such a common operation. Btw, things have changed. From Java 8 onwards you can do this easily. JDK 8 has provided a utility class called StringJoiner as well as added a join() method into String class to convert a List, Set, Collection, or even array of String objects to a comma-separated String in Java.

This method accepts a delimiter and an Iterable which contains String. Since all Collection classes implement Iterable, it's possible to join all String from a List or Set using a delimiter like a comma, colon, or even space.

Btw, like most things, you need to know some small details. For example, if your List or Set contains null then a "null" string will be added into the final String, so please beware of that. If you don't want null, it's better to remove any nulls before joining strings from the list.

By the way, Java 8 is full of such excellent features which are often get ignored in the limelight of lambda expression, Stream API, new Date Time API, and other significant features.

1. List of String to Comma Separated String in Java

Here is an example of converting a list and set to a comma-separated String in Java 8:

package tool;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

* A simple Java Program to to remove duplicates from Stream in Java 8
* This example uses Stream.distinct() method to remove
* duplicates. 
public class Hello {

public static void main(String args[]) {

List<String> books = Arrays.asList("Effective Java", "Clean Code", 
"Java Concurrency in Practice");

String bookString = String.join(",", books);

Set<String> authors = new HashSet<>(Arrays.asList("Josh Bloch", "Uncle Bob Martin",
 "Brian Goetz"));

String authorString = String.join(",", books);


[Effective Java, Clean Code, Java Concurrency in Practice]
Effective Java,Clean Code,Java Concurrency in Practice
[Josh Bloch, Uncle Bob Martin, Brian Goetz]
Effective Java,Clean Code,Java Concurrency in Practice

You can see that if you directly print the list or set, all the elements are written inside a bracket. Since we just need a CSV or comma-separated String, we need to use the String.join() method, and that's what we have done in this example.

The lines:
String bookString = String.join(",", books);
String authorString = String.join(",", books);

Are the two lines which are doing all the work of joining all String from the list and set into one separated by a comma. If you want, you can change the delimiter to colon or space to create a different String based upon your requirement.

By the way, Java 8 is full of such hidden features which are often get ignored in the limelight of lambda expression, Stream API, and other major features. If you are interested to learn more about new Java 8 features, I suggest you go through What's New in Java 8 on Udemy, one of the comprehensive courses to learn Java.

How to Convert a List or Set of String to comma separated String (CSV) in Java 8

Important points

1. The String.join() method is only available from JDK 8 onwards. So, if you need to convert a list or set to a delimited String in an earlier version, you can loop through all the elements and join them manually using a StringBuffer or StringBuilder.

2. A "null" as String will be added to your output if your list or set contains a null element.

3. Since String.join() method accepts an Iterable, you can pass any Collection implementation to it, e.g. List, Set, ArrayList, LinkedList, Vector, HashSet, TreeSet, LinkedHashSet, etc.

4. The String.join() is a static method, so you don't need a String object to call this one.

That's all about how to convert a list of String to a comma-separated String in Java. There are many ways to complete this task, but as I said, prefer String.join() if you are using Java 8 or higher version. You can also change the delimiter to any character or String you want. For example, if your String is separated by a colon instead of a comma, then you can just use a colon, and it will work like this. Just beware of null though.

Other Java 8  Lambda and Stream Tutorials You may like

Thanks for reading this article so far. If you like this article, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.