Convert Array to HashSet in Java
Last Updated : 09 Jan, 2023
The Single Data structure cannot be able to fulfill the requirements of the programmers that's why there are a lot of inbuilt Data-Structures in Programming languages.
Arrays are the most commonly used Data-Structure in most programming languages. The advantage of this Data-Structure is O(1) accessing the elements of the Arrays with the help of indexing but the most common disadvantages are we cannot change the size of the array after creating and the deletion of the elements is a complicated process in Arrays.
Sets: In Java, Any group of individual objects which are represented as a single unit is known as the collection of the objects. In Java, a separate framework named the “Collection Framework” has been defined in JDK 1.2 which holds all the collection classes and interface. Sets are classified into two parts sorted set and unsorted set both have advantage and disadvantage. Sorted Set i.e TreeSet sort its unique elements but the time-complexity of TreeSet is O(N log N) but unsorted Sets such as HashSet and LinkedSet do change the order of the elements but the difference between the HashSet and LinkedSet is Random Order of its elements.

Examples:
Input : Array: [1, 2, 3, 4, 5, 6] Output: Set: [1, 2, 3, 4, 5, 6] Input : Array: [a, b, c, d] Output: Set: [a, b, c, d]
Approach: Brute Force or Naive Method
Create an empty set (HashSet if unsorted elements require) Iterate the elements of the array and add one by one to the set.
Example:
Java // Convert array to HashSet in Java import java.io.*; import java.util.Iterator; // Importing Set libraries import java.util.Set; import java.util.HashSet; class GFG { // Function to convert array to set static Set<Integer> convert(int[] array) { // Hash Set Initialisation Set<Integer> Set = new HashSet<>(); // Iteration using enhanced for loop for (int element : array) { Set.add(element); } // returning the set return Set; } // Function to print the set static void print(Set<Integer> Set) { // Implement to iterator the Set Iterator<Integer> _iterator = Set.iterator(); // Iterate the elements of Set while (_iterator.hasNext()) { // print the element of the Set System.out.print(_iterator.next() + " "); } } public static void main(String[] args) { // Array taken for consideration int array[] = { 1, 2, 3, 4, 5, 6 }; // Calling function to convert the array Set<Integer> Set = convert(array); // print the set print(Set); } }
Approach 2:
Using Java 8 Stream API: HashSet constructor can take another collection object to construct a new set containing the elements of the specified array.
- Get the Array to be converted.
- Convert the array to Stream
- Convert the Stream to Set using Collectors.toSet()
- Collect the formed set using the collect() method
- Return the formed Set.
Example:
Java // Convert Array to HashSet in Java import java.util.*; import java.util.stream.*; class GFG { // Generic function to convert array to set public static <T> Set<T> convertArrayToSet(T array[]) { // create a set from the Array return Arrays.stream(array).collect( Collectors.toSet()); } public static void main(String args[]) { // Create an Array String array[] = { "Geeks", "forGeeks", "A computer Portal" }; // Print the Array System.out.println("Array: " + Arrays.toString(array)); // convert the Array to Set Set<String> set = convertArrayToSet(array); // Print the Set System.out.println("Set: " + set); } }
OutputArray: [Geeks, forGeeks, A computer Portal] Set: [A computer Portal, Geeks, forGeeks]
Similar Reads
Convert HashSet to array in Java Java HashSet class is used to create a collection that uses a hash table for the storage of elements. It inherits AbstractSet class and implements Set Interface. The key points about HashSet are: HashSet contains unique elements only.HashSet allows null values.The insertion of elements in a HashSet
2 min read
Convert HashSet to a ArrayList in Java ArrayList class is a resizable array, present in java.util package. The difference between an array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elements can b
4 min read
How to Convert ArrayList to HashSet in Java? ArrayList: In Java, ArrayList can have duplicates as well as maintains insertion order. HashSet: HashSet is the implementation class of Set. It does not allow duplicates and uses Hashtable internally. There are four ways to convert ArrayList to HashSet : Using constructor.Using add() method by itera
3 min read
Convert a HashSet to a LinkedList in Java In Java, HashSet and LinkedList are linear data structures. These are majorly used in many cases. There may be some cases where we need to convert HashSet to LinkedList. So, in this article, we will see how to convert HashSet to LinkedList in Java. Java Program to Convert a HashSet to a LinkedList T
2 min read
How to Convert a HashSet to JSON in Java? In Java, a HashSet is an implementation of the Set interface that uses a hash table to store elements. It allows fast lookups and does not allow duplicate elements. Elements in a HashSet are unordered and can be of any object type. In this article, we will see how to convert a HashSet to JSON in Jav
2 min read