Convert HashSet to array in Java
Last Updated : 11 Dec, 2020
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 is based on a hashcode.
- HashSet is best used for searching problems.
There are two ways of converting HashSet to the array:
- Traverse through the HashSet and add every element to the array.
- To convert a HashSet into an array in java, we can use the function of toArray().
Method 1: By traversing the set add elements to the array
We can traverse the Set using a simple for loop and then add elements one by one to the array.
Java // Java program to convert HashSet to array import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { HashSet<String> set = new HashSet<String>(); set.add("1"); set.add("13"); set.add("27"); set.add("87"); set.add("19"); System.out.println("Hash Set Contains :" + set); String arr[] = new String[set.size()]; int i=0; // iterating over the hashset for(String ele:set){ arr[i++] = ele; } for (String n : arr) System.out.println(n); } }
OutputHash Set Contains :[1, 13, 27, 19, 87] 1 13 27 19 87
Method 2: Using toArray() method
Syntax:
public Object[] toArray() or public <T> T[] toArray(T[] a)
Parameters: This method either accepts no parameters or it takes an array T[] a as a parameter which is the array into which the elements of the list are to be stored if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Return Value: The function returns an array containing all the elements in this list.
Java // Java program to convert HashSet to array import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { HashSet<String> set = new HashSet<String>(); set.add("1"); set.add("13"); set.add("27"); set.add("87"); set.add("19"); System.out.println("Hash Set Contains :" + set); String arr[] = new String[set.size()]; // toArray() method converts the set to array set.toArray(arr); for (String n : arr) System.out.println(n); } }
OutputHash Set Contains :[1, 13, 27, 19, 87] 1 13 27 19 87
Similar Reads
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
Program to convert Array to Set in Java Array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In the case of primitives data types, the actual values are stored in contiguous memory locations. In cas
7 min read