Java List to String

In this post, we will see how to convert List to String in java.
Following are the ways to convert list into String:

Using StringBuilder

We can use StringBuilder class to convert List to String. StringBuilder is the best approach if you have other than String Array, List.We can add elements in the object of StringBuilder using the append() method while looping and then convert it into string using toString() method of String class at the end.

Example:

OUTPUT:

Elements of list are:[Mary, Martin, John, Newton] Converted String is Mary,Martin,John,Newton,

Using + operator

This is not an efficient way so, normally avoid it.We use concate(+) operator to add one by one element from array inside loop and assign it into String.

Example:

OUTPUT:

Elements of list are:[Mary, Martin, John, Newton] Converted String is Mary,Martin,John,Newton,

Using String’s join method in java 8

String’s join method is introduced in Java 8 . This method is very handy to create delimited separated String from list.

This approach will work only for List of String. In case, if you have list of Double, then you can use java 8‘s Collectors to join the String.
Here is an example:

Output:

Elements of list are:[34.2, 29.7, 40.2, 50.4] Converted String is: 34.2,29.7,40.2,50.4

As you can see, we have used Collectors.joining(",") to join elements of String by ,

Using Apache common lang3

You can also use Apache common’s StringUtils to convert list to String.
Here is the dependency which you need to add for Apache common lang3 in pom.xml.

Here is the example:

Elements of list are:[1, 2, 3, 4] Converted String is: 1*2*3*4

Using Guava library

You can also use Guava’s Joiner class to convert list to String.
Here is the dependency which you need to add for guava in pom.xml.

Here is the example:

Elements of list are:[1, 2, 3, 4] Converted String is: 1*2*3*4

As you can see, we have used com.google.common.base.Joiner class to join elements of String by *

Print elements of list as String

Sometimes, we just need to print elements of the list. We can simply use toString() method
Let’s understand with the help of example.

Output:

Elements of list are:[1, 2, 3, 4]

Did you notice we did not even call toString() method on intList object?
JVM internally calls toString() method of type of element within this list. In this case, we have list of Integers, so it will call Integer’s toString() method.

In case, you have custom object, then you should override toString() method, else it will give you unexpected results.
Let’s see with the help of example
Create a simple class named Color.java

Create main class named PrintListOfColorsMain.java

Output:

If you notice, we don’t have toString() method in Color class, that’s why it is calling Object’s toString() method.

We need to override toString() method in Color class and we will get informative output.

When you run PrintListOfColorsMain again, you will get below output:

[Color [name:Red HtmlCode:#FF0000], Color [name:Blue HtmlCode:0000FF], Color [name:White HtmlCode:#FFFFFF], Color [name:Green HtmlCode:#008000]]

As you can see, we have much meaningful output now.

That’s all about converting List to String in java.

Was this post helpful?

Related Posts

  • 09 January

    Convert set to String in Python

    Table of ContentsWays to convert set to string in PythonUsing the str() functionUsing the repr() functionUsing the join() functionUsing the map() functionConclusionWas this post helpful? Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python. Ways to convert set to […]

  • Convert String to Array in java
    11 December

    Convert String to Array in Java

    Table of ContentsHow to convert String to Array in JavaUsing toArray() method of SetUsing the split() method of String classUsing StringTokenizor classUsing the split() method of StringUtils classUsing split() method of Pattern classConclusionWas this post helpful? When developing applications in Java there are many cases where we will find ourselves converting data from one type […]

  • Convert float to int in Python
    19 June

    Convert float to int in Python

    Table of ContentsUsing the int() function to convert float to int in PythonUsing the math module functions to convert float to int in PythonUsing ceil()Using trunc()Using floorWas this post helpful? Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions. This […]

  • Java BigDecimal to Double
    17 May

    Java BigDecimal to Double

    Learn about how to convert BigDecimal to Double in java

  • Java Set to Array
    12 May

    Java Set to Array

    Table of Contents1. Using Java 8’s Stream2. Using toArray()3. Using toArray(IntFunction) [Java 11]4. Using System.arraycopy()5. Using Arrays.copyOf6. Using simple iteration7. Using Guava library7.1 Using FluentIterable7.2 Using IterablesWas this post helpful? In this post, we will learn java set to array conversion. There are many ways to convert set to an array. 1. Using Java 8’s […]

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.DateWas this post helpful? In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.