

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.ġ.
#CONVERT STRING TO LIST JAVA 8 FULL#
If you want, you can change the delimiter to colon or space to create a different String based upon your requirement.īy 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. join( ",", books) Īre the two lines which are doing all the work of joining all String from the list and set into one separated by a comma. 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. You can see that if you directly print the list or set, all the elements are written inside a bracket. */ public class Hello Įffective Java,Clean Code,Java Concurrency in Practice distinct() method to remove * duplicates. Set /** * * A simple Java Program to to remove duplicates from Stream in Java 8 * This example uses Stream. Here is an example of converting a list and set to a comma-separated String in Java 8: List of String to Comma Separated String in Java If you don't want null, it's better to remove any nulls before joining strings from the list.īy 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.ġ. 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. 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.ītw, like most things, you need to know some small details. This method accepts a delimiter and an Iterable which contains String. 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. From Java 8 onwards you can do this easily. 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. 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. Before Java 8, it was not straightforward to convert a list of String to a comma-separated String.
