The Wayback Machine - https://web.archive.org/web/20240818224327/https://www.geeksforgeeks.org/convert-a-list-of-string-to-a-comma-separated-string-in-java/
Open In App

Convert a List of String to a comma separated String in Java

Last Updated : 21 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a List of String, the task is to convert the List to a comma separated String in Java. 

Examples:

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.

  1. Get the List of String.
  2. Form a comma separated String from the List of String using join() method by passing comma ‘, ‘ and the list as parameters.
  3. Print the String.

Below is the implementation of the above approach: 

Program: 

Java




// Java program to convert List of String
// to comma separated String
 
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Get the List of String
        List<String>
            list = new ArrayList<>(
                Arrays
                    .asList("Geeks",
                            "ForGeeks",
                            "GeeksForGeeks"));
 
        // Print the List of String
        System.out.println("List of String: " + list);
 
        // Convert the List of String to String
        String string = String.join(",", list);
 
        // Print the comma separated String
        System.out.println("Comma separated String: "
                           + string);
    }
}


Output

List of String: [Geeks, ForGeeks, GeeksForGeeks]
Comma separated String: Geeks,ForGeeks,GeeksForGeeks

Previous Article
Next Article

Similar Reads

Convert String into comma separated List in Java
Given a String, the task is to convert it into comma separated List. Examples: Input: String = "Geeks For Geeks" Output: List = [Geeks, For, Geeks] Input: String = "G e e k s" Output: List = [G, e, e, k, s] Approach: This can be achieved by converting the String into String Array, and then creating an List from that array. However this List can be
2 min read
Convert a Set of String to a comma separated String in Java
Given a Set of String, the task is to convert the Set to a comma separated String in Java. Examples: Input: Set&lt;String&gt; = ["Geeks", "ForGeeks", "GeeksForGeeks"] Output: "Geeks, For, Geeks" Input: Set&lt;String&gt; = ["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 fol
1 min read
Convert ArrayList to Comma Separated String in Java
ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. In order to convert ArrayList to a comma-separated String, these are the approaches available in Java as listed and proposed below as follows: Earlier before Java 8 there were only standard methods available in order for this
6 min read
How to Convert Comma Separated String to HashSet in Java?
Given a Set of String, the task is to convert the Set to a comma-separated String in Java. Examples: Input: Set&lt;String&gt; = ["Geeks", "ForGeeks", "GeeksForGeeks"] Output: "Geeks, For, Geeks" Input: Set&lt;String&gt; = ["G", "e", "e", "k", "s"] Output: "G, e, e, k, s" There are two ways in which we can convert comma separated string to Java Hash
2 min read
How to Convert a Comma-Separated String into an ArrayList in Java ?
In Java, a String is a sequence of characters. They are created using the String class and once a string is created, they are immutable which means that their value cannot be changed. In this article, we will be learning how to convert a comma-separated String into an ArrayList in Java. Syntax of ArrayList: ArrayList&lt;datatype&gt; name = new Arra
3 min read
How to Take Input From User Separated By Space in Java ?
There are 2 methods to take input from the user which are separated by space which are as follows: Using BufferedReader Class and then splitting and parsing each valueUsing nextInt( ) method of Scanner class Let us discuss both the methods one by one in order to get a better understanding by implementing the same clean java programs. Method 1: Usin
3 min read
Program to convert List of Integer to List of String in Java
The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes. Using Java 8 Stream API: A Stream is
3 min read
Program to convert List of String to List of Integer in Java
The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector, and Stack classes. 1. Using Java 8 Stream API: A Strea
3 min read
How to Convert java.sql.Date to java.util.Date in Java?
If we have the Date object of the SQL package, then we can easily convert it into an util Date object. We need to pass the getTime() method while creating the util Date object. java.util.Date utilDate = new java.util.Date(sqlDate.getTime()); It will give us util Date object. getTime() method Syntax: public long getTime() Parameters: The function do
1 min read
Different Ways to Convert java.util.Date to java.time.LocalDate in Java
Prior to Java 8 for handling time and dates we had Date, Calendar, TimeStamp (java.util) but there were several performance issues as well as some methods and classes were deprecated, so Java 8 introduced some new concepts Date and Time APIs' (also known as Joda time APIs') present in java.time package. Java 7: Date, Calendar, TimeStamp present ins
3 min read
How to Convert java.util.Date to java.sql.Date in Java?
Date class is present in both java.util package and java.sql package. Though the name of the class is the same for both packages, their utilities are different. Date class of java.util package is required when data is required in a java application to do any computation or for other various things, while Date class of java.sql package is used whene
3 min read
Convert List of Characters to String in Java
Given a list of characters. In this article, we will write a Java program to convert the given list to a string. Example of List-to-String ConversionInput : list = {'g', 'e', 'e', 'k', 's'} Output : "geeks" Input : list = {'a', 'b', 'c'} Output : "abc" Strings - Strings in Java are objects that are supported internally by a char array. Since arrays
4 min read
Convert a String to a List of Characters in Java
Given a String, the task is to convert it into a List of Characters in Java. Examples: Input: String = "Geeks" Output: [G, e, e, k, s] Input: String = "GeeksForGeeks" Output: [G, e, e, k, s, F, o, r, G, e, e, k, s] Below are the various ways to do so: Naive Method Approach: Get the String. Create an empty List of Characters. Add each character of S
3 min read
Convert String or String Array to HashMap In Java
Considering a string object that contains the student name and roll number separated by a comma, and each student contains the name and roll number separated by a colon. Now need is to convert the String to a Map object so that each student roll number becomes the key of the HashMap, and the name becomes the value of the HashMap object. In order to
4 min read
Convert Set of String to Array of String in Java
Given a Set of Strings, the task is to convert the Set into an Array of Strings in Java. Examples: Input: Set&lt;String&gt;: ["ForGeeks", "A Computer Portal", "Geeks"] Output: String[]: ["ForGeeks", "A Computer Portal", "Geeks"] Input: Set&lt;String&gt;: ["G", "e", "k", "s"] Output: String[]: ["G", "e", "k", "s"] Method 1: Naive Method. Get the Set
6 min read
Convert an ArrayList of String to a String Array in Java
In Java, as we all know ArrayList class is derived from the List interface. Here we are given an ArrayList of strings and the task is to convert the ArrayList to a string array. Illustration: Input : ArrayList = [ "Geeks", "for", "Geeks" ] Output: String[] = {"Geeks", "for", "Geeks"}Input : ArrayList = [ "Jupiter", "Saturn", "Uranus", "Neptune", "S
3 min read
Java Program to Convert String to String Array Using Regular Expression
Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are few areas of strings where Regex is widely used to define the constraints. Regular Expressions are provided under java. util.regex package. This consists of 3 c
3 min read
Java Program to Convert String to String Array
Given a String, the task is to convert the string into an Array of Strings in Java. It is illustrated in the below illustration which is as follows: Illustration: Input: String : "Geeks for Geeks" Output: String[]: [Geeks for Geeks]Input: String : "A computer science portal" Output: String[] : [A computer science portal] Methods: They are as follow
6 min read
Java Program to Convert Integer List to Integer Array
There are many ways to convert integer List to ArrayList where in this article we will be discussing out 2 approaches as below: Using concept od streams in Java8Using Apache Commons LangUsing Guava Library Method 1: Using concept od streams in Java8 So, first, we will get to know about how to convert integer list to array list using java 8. In java
3 min read
Convert List to Set in Java
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. Few important features of the Java Set interface are as follows: The set interface is an unordered collection of objects in which duplicate values cannot be stored.The
5 min read
Program to Convert List to Stream in Java
The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes. A Stream is a sequence of objects that support
3 min read
Program to Convert Set to List in Java
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, i
4 min read
Java Program to Convert an Array into a List
Given an array. Your task is to convert the given array into a list in Java. Examples: Input: Array: [Geeks, forGeeks, A computer Portal] Output: List: [Geeks, forGeeks, A computer Portal] Input: Array: [1, 2, 3, 4, 5] Output: List: [1, 2, 3, 4, 5] Arrays: An array is a group of like-typed variables that are referred to by a common name. An array c
6 min read
Program to Convert List to Map in Java
The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes. The java.util.Map interface represents a mappi
5 min read
Convert an Iterator to a List in Java
Given an Iterator, the task is to convert it into List in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Below are the various ways to do so: Naive Approach: Get the Iterator. Create an empty list. Add each element of the iterator to the list us
2 min read
Program to Convert a Vector to List in Java
Given a Vector, the task is to Convert Vector to List in Java Examples: Input: Vector: [1, 2, 3, 4, 5] Output: List: [1, 2, 3, 4, 5] Input : Vector = [a, b, c, d, e, f] Output : List = [a, b, c, d, e, f] Using Collections.list() method Syntax: List list = Collections.list(vec.elements()); Approach: Get the Vector Convert into list using Collections
2 min read
How to Convert LinkedHashMap to List in Java?
LinkedHashMap is predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, in LinkedHashMap insertion order is preserved. We to convert LinkedHashMap to ArrayList. A Map store data in pair of Key and Value while converting a LinkedHashMAp to ArrayList we will store keys of Map in a separate List, si
2 min read
How to Convert all LinkedHashMap Key-Value pairs to List in Java?
The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements can be accessed in their insertion order. We have
2 min read
How to Convert all LinkedHashMap Values to a List in Java?
The task is to convert all LinkedHashMap values to a list in java. LinkedHashMap is an implementation of a Map. The Map and List are two different data structures. The Map stores key-value pairs while the List is an ordered collection of elements. To convert all values of the LinkedHashMap to a List using the values() method. The values() method of
2 min read
Java Program to Convert List to HashSet
The List interface provides a way to store the ordered collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. The HashSet class permits the null element. The class also offers constant time performance for the basic
4 min read
three90RightbarBannerImg