Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
How to convert integer set to int array using Java?
A collection object in Java is the one which stores references of other objects in it. The java.util package provides the classes and interfaces for collections. There are four main collection interfaces namely Set Lists, Queues, Maps.
Set − The set object is a collection which stores group of elements, it grows dynamically and it does not allow duplicate elements.
HashSet and LinkedHashSet are the classes that implements Set interface. You can create a Set object by implementing either of these classes.
Example
import java.util.HashSet;
public class SetExample {
public static void main(String args[]) {
//Instantiating the HashSet
HashSet<String> hashSet = new HashSet<String>();
//Populating the HashSet
hashSet.add("Mango");
hashSet.add("Apple");
hashSet.add("Cherries");
hashSet.add("Banana");
System.out.println(hashSet);
}
}
Output
[Apple, Mango, Cherries, Banana]
Converting a Set object to an array
You can convert a set object into an array in several ways −
Add each element − You can add each element of the Set object to the array using the foreach loop.
Example
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String args[]) {
//Instantiating the HashSet
Set<Integer> hashSet = new HashSet<Integer>();
//Populating the HashSet
hashSet.add(1124);
hashSet.add(3654);
hashSet.add(7854);
hashSet.add(9945);
System.out.println(hashSet);
//Creating an empty integer array
Integer[] array = new Integer[hashSet.size()];
//Converting Set object to integer array
int j = 0;
for (Integer i: hashSet) {
array[j++] = i;
}
}
}
Output
[1124, 3654, 9945, 7854]
Using the toArray() method − The toArray() method of the Set interface accepts an array, populates it with all the elements in the current set object and, returns it. using this method, you can convert a Set object to an array.
Example
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String args[]) {
//Instantiating the HashSet
Set<Integer> hashSet = new HashSet<Integer>();
//Populating the HashSet
hashSet.add(1124);
hashSet.add(3654);
hashSet.add(7854);
hashSet.add(9945);
//Creating an empty integer array
Integer[] array = new Integer[hashSet.size()];
//Converting Set object to integer array
hashSet.toArray(array);
System.out.println(Arrays.toString(array));
}
}
Output
[1124, 3654, 9945, 7854]
Using Java8: Since Java8 Streams are introduced and these provide a method to convert collection objects to array.
Example
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String args[]) {
//Instantiating the HashSet
Set<Integer> hashSet = new HashSet<Integer>();
//Populating the HashSet
hashSet.add(1124);
hashSet.add(3654);
hashSet.add(7854);
hashSet.add(9945);
System.out.println(hashSet);
//Creating an empty integer array
Integer[] array = hashSet.stream().toArray(Integer[]::new);
System.out.println(Arrays.toString(array));
}
}
Output
[1124, 3654, 9945, 7854]
