Convert List of Strings to Comma-Separated String in Java



In this article, we will understand how to convert a list of strings to comma separated string in Java. A list is an ordered collection that allows us to store and access elements sequentially. It contains index-based methods to insert, update, delete, and search the elements. It can also have the duplicate elements.

Problem Statement

Write a Java program to convert a list of strings into a comma-separated string. Below is a demonstration of the same ?

Input 

Input list: [Java, Scala, Python]

Output

The list with comma-separated elements: Java, Scala, Python

Different approaches

Following are the different approaches to converting a list of strings to comma separate string

Using main() method

Following are the steps to convert a list of string to comma separated string using encapsulation ?

  • First we will import all the classes from java.util package.
  • We will define the main method in a Demo class.
  • We will declare a List named input_list and initialize it with strings.
  • Use the String.join() method to concatenate the list elements with commas.
  • Print the original list and the comma-separated string.

Example

Here, we bind all the operations together under the ?main' method ?

import java.util.*;
public class Demo {
   public static void main(String args[]){
      List input_list =
         new ArrayList<>( Arrays .asList("Java", "Scala", "Python"));
      System.out.println("The input_list is defined as: " + input_list);
      String string = String.join(", ", input_list);
      System.out.println("The list with comma separated elements: " + string);
   }
}

Output

The input_list is defined as: [Java, Scala, Python]
The list with comma-separated elements: Java, Scala, Python

Using encapsulation

Following are the steps to convert a list of strings to comma separated strings using encapsulation ?

  • Import the necessary classes from java.util package.
  • Define the main method in a Demo class.
  • Create a List named input_list and initialize it with strings.
  • Print the original list.
  • Call the add_comma method, passing the list as an argument.
  • Inside the add_comma method, use String.join() to create a comma-separated string.
  • Print the resulting string from the add_comma method.

Example

Here, we encapsulate the operations into functions exhibiting object-oriented programming ?

import java.util.*;
public class Demo {
   static void add_comma(List<String> input_list){
      String string = String.join(", ", input_list);
      System.out.println("The list with comma separated elements: " + string);
   }
   public static void main(String args[]){
      List<String> input_list =
         new ArrayList<>( Arrays .asList("Java", "Scala", "Python"));
      System.out.println("The list is defined as: " + input_list);
      add_comma(input_list);
   }
}

Output

The list is defined as: [Java, Scala, Python]
The list with comma separated elements: Java, Scala, Python
Updated on: 2024-11-19T18:25:22+05:30

698 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements