- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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]
- Related Articles
- Program to convert set of Integer to Array of Integer in Java
- How to convert Integer array list to integer array in Java?
- Java Program to convert int array to IntStream
- How to convert Integer array list to float array in Java?
- How to convert an object array to an integer array in Java?
- Program to convert Set of Integer to Set of String in Java
- Program to convert set of String to set of Integer in Java
- How to convert List to int[] in Java?
- How to convert int to String in java?
- How to Convert Int to Double in Java?
- How to Convert Int to Long in Java?
- How to convert an Array to a Set in Java?
- Convert String to Java int
- Java program to convert an Array to Set
- Program to convert Array to Set in Java
