Convert a String to a List split by new line

In this post we will show how to convert a string to a List by splitting the string based on new lines and vice versa.

Convert a String to a List

The following example will convert a string to a list split by new line.

  • We have split the string using the split() method
  • The collect() method will create a list from the split content.

Running the above code produces the following output:

Convert List to String:

The following example shows how to convert a List to a String with a new line separator per list entry.

  • The Collectors.joining() will join the stream items based on your delimiter and in this case it will be a new line.

Running the above code produces the following output

I hope you found this post useful, and it has helped you solve your development issue.

Similar posts:

  1. String Basics
  2. Convert byte array to string
  3. Parse JSON string to Java Object

References:

  1. Java String API Documentation
  2. Java 8 Streams tutorials

 

Leave a Reply

Your email address will not be published. Required fields are marked *