BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

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

Last Updated: September 23, 2022 by Chaitanya Singh | Filed Under: java

Problem description: We have given a Set that contains String elements. Our task is to convert this set of string to a comma separated string in Java.

For example:

Input: Set<String> = ["Apple", "Orange", "Mango"]
Output: "Apple, Orange, Mango"

Input: Set<String> = ["J", "a", "v", "a"]
Output: "J, a, v, a"

Example 1: Using String.join() method

We can do this conversion using join() method of String class. The string join() method returns a string by combining all the elements of an iterable collection(list, set etc), separated by the given separator.

Here we will pass the set to the String join() method and separator as comma (,). This will join all the set elements with the comma and return a comma separated string.

import java.util.*;
public class JavaExample {
  public static void main(String args[])
  {
    // Set of Strings
    Set<String> animalSet = new HashSet<>();
    animalSet.add("Lion");
    animalSet.add("Tiger");
    animalSet.add("Monkey");
    animalSet.add("Rabbit");

    // Displaying Set elements
    System.out.println("Set of Strings: " + animalSet);

    // Convert the Set of Strings to comma separated String
    String str = String.join(", ", animalSet);

    // Display the Comma separated String as output
    System.out.println("Comma Separated String: "+ str);
  }
}

Output:

Set of Strings to comma separated string in Java

Example 2: Using Stream API

Stream API was introduced in java 8. In the following example, we are using stream API for the conversion.

import java.util.*;
import java.util.stream.Collectors;
public class JavaExample {
  public static void main(String args[])
  {
    // Set of Strings
    Set<String> animalSet = new HashSet<>();
    animalSet.add("Lion");
    animalSet.add("Tiger");
    animalSet.add("Monkey");
    animalSet.add("Rabbit");

    // Displaying Set elements
    System.out.println("Set of Strings: " + animalSet);

    // Using Stream API for conversion
    // collect() method returns the result of the
    // intermediate operations performed on the stream
    String str = animalSet.stream().collect(
            Collectors.joining(","));

    // Display the Comma separated String as output
    System.out.println("Comma Separated String: "+ str);
  }
}

Output:

Set of Strings: [Monkey, Lion, Rabbit, Tiger]
Comma Separated String: Monkey,Lion,Rabbit,Tiger
Main Article: Collections in Java

Top Related Articles:

  1. Java 9 – @SafeVarargs Annotation (with examples)
  2. Java 8 Interface Changes – default method and static method
  3. Java 8 Stream Tutorial
  4. Split String by Newline in Java
  5. Java String substring() Method with examples

Tags: Collections, Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap