Java Convert List to String

Previous
Next

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 a best approach if you have other than String Array,List.We can add elements in the object of StringBuilder using 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.

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

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog