The ways to convert from List of String object to String object in Java

In Java, we have a lot of ways to convert List of String object to String object with a delimiter between the elements in List object. In this tutorial, I will share with you all some ways as below:

The first one, we will use StringBuilder object in Java.

We will read all elements in the List, one by one and use StringBuilder to add the delimiter among all elements. Note that, with the first element, we will not add the delimiter!

In detail, the method can be written as below:

Example:

The ways to convert from List of String object to String object in Java

The second one, we will use collect() method of Stream object which was introduced since Java 8.

Example:

The ways to convert from List of String object to String object in Java

The third one, we will use join() static method of String object.

Since Java 8, Java introduced a new method named join() in String object, help us can convert from List object to String object easily.

Example:

The ways to convert from List of String object to String object in Java

Finally, we can use an existing library Apache Commons Lang of Apache Foundation.

This library provides a static method, named join() in StringUtils class to help us can convert a List object to String object simply and easily. Because of its static method, then we only need call:

Example:

The ways to convert from List of String object to String object in Java

Add Comment