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 Comma Separated String to HashSet in Java

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

In this guide, we will discuss, how to convert a comma separated string to HashSet in Java. We will be using the String split() method to split the string into multiple substrings and then these substrings will be stored as HashSet elements.

Input String: "text1,text2,text3"
Output HashSet Elements: ["text1", "text2", "text3"]

Input String: "Apple,Orange,Banana"
Output HashSet Elements: ["Apple", "Orange", "Banana"]

Example 1: Using String split() method and Arrays

In this example, we will split the given comma separated string to HashSet using String.split() method. The Steps are as follows:

  • Split the given comma separated to substrings using split() method.
  • Store the returned string elements in a String array.
  • Pass this array to a List by using Arrays.asList() method which converts string array to a List.
  • Convert the List obtained in previous step to HashSet by passing it in the HashSet constructor.
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class JavaExample {

  public static void main(String[] args) {

    String str = "text1,text2,text3";

    // split the string by comma - and store the substrings
    // in the string array subStr
    String[] subStr = str.split(",");

    // convert string array to the List using asList() method
    List<String> strList = Arrays.asList(subStr);

    // Pass the list obtained in the previous step to HashSet
    // constructor to get an equivalent Set
    HashSet<String> hSet = new HashSet<>(strList);

    System.out.println("Input string: "+str);
    System.out.println("HashSet Elements: " + hSet);
  }
}

Output:

Convert string with commas to HashSet Output

Example 2: Using Java 8 Stream API

Java 8 introduced a new concept of streams. You can use Java 8 stream to convert the given comma separated string to HashSet.

import java.util.*;
import java.util.stream.*;
public class JavaExample {

  public static void main(String[] args)
  {
    String str = "AA,BB,CC,DD,EE";

    //Using Java 8 Stream to convert the string to HashSet
    //Pass the delimiter between double quotes,
    // here we have comma as a delimiter
    Set<String> hSet
            = Stream.of(str.trim().split(","))
            .collect(Collectors.toSet());

    System.out.println("HashSet Elements: " + hSet);
  }
}

Output:

HashSet Elements: [AA, BB, CC, DD, EE]
❮ Java HashSetJava String ❯

Top Related Articles:

  1. LinkedHashMap in Java
  2. Java String substring() Method with examples
  3. Split String by Newline in Java
  4. Java 9 – @SafeVarargs Annotation (with examples)
  5. How to Convert ArrayList to HashSet in Java

Tags: Collections, Java-HashSet, 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