Convert a List of String to a comma separated String in Java
Given a List of String, the task is to convert the List to a comma separated String in Java.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: List<String> = ["Geeks", "ForGeeks", "GeeksForGeeks"] Output: "Geeks, For, Geeks" Input: List<String> = ["G", "e", "e", "k", "s"] Output: "G, e, e, k, s"
Approach: This can be achieved with the help of join() method of String as follows.
- Get the List of String.
- Form a comma separated String from the List of String using join() method by passing comma ‘, ‘ and the list as parameters.
- Print the String.
Below is the implementation of the above approach: