Program to Convert Set of Integer to Array of Integer in Java
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element.
A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Java 8 Stream API can be used to convert Set
- Using Java 8 Stream: A Stream is a sequence of objects that support various methods which can be pipelined to produce the desired result. Java 8 Stream API can be used to convert Set
to int[]. Algorithm:
- Get the set of integers.
- Convert Set of Integer to Stream of Integer.
This is done using Set.stream(). - Convert Stream of Integer to IntStream.
- Convert IntStream to int[].
This is done using IntStream.toArray(). - Return/Print the integer array int[]
Program:
// Java Program to convert// Set<Integer> to int[] in Java 8importjava.util.*;importjava.util.stream.*;importjava.util.function.Function;classGFG {// Function to convert Set of Integer to Set of Stringpublicstaticint[] convertIntSetToStringSet(Set<Integer> setOfInteger){returnsetOfInteger.stream().mapToInt(Integer::intValue).toArray();}publicstaticvoidmain(String args[]){// Create a set of integersSet<Integer> setOfInteger =newHashSet<>(Arrays.asList(1,2,3,4,5));// Print the set of IntegerSystem.out.println("Set of Integer: "+ setOfInteger);// Convert Set of integers to set of Stringint[] intArray = convertIntSetToStringSet(setOfInteger);// Print the set of StringSystem.out.println("Array of Integer: "+ Arrays.toString(intArray));}}Output:Set of Integer: [1, 2, 3, 4, 5] Array of Integer: [1, 2, 3, 4, 5]
- Using Guava Ints.toArray(): Guava Ints.toArray() can be used to convert set of integer to an array of integer.
Algorithm:
- Get the set of integers
- Create an array of integer by Ints.toArray() method of Guava library, by passing the set of integers as the argument to this method.
- Return/Print the created integer array int[]
Program:
// Java Program to convert// Set<Integer> to int[] in Java 8importcom.google.common.primitives.Ints;importjava.util.*;importjava.util.stream.*;importjava.util.function.Function;classGFG {// Function to convert Set of Integer// to Set of Stringpublicstaticint[] convertIntSetToStringSet(Set<Integer> setOfInteger){returnInts.toArray(setOfInteger);}publicstaticvoidmain(String args[]){// Create a set of integersSet<Integer> setOfInteger =newHashSet<>(Arrays.asList(1,2,3,4,5));// Print the set of IntegerSystem.out.println("Set of Integer: "+ setOfInteger);// Convert Set of integers to set of Stringint[] intArray = convertIntSetToStringSet(setOfInteger);// Print the set of StringSystem.out.println("Array of Integer: "+ Arrays.toString(intArray));}}Output:
Set of Integer: [1, 2, 3, 4, 5] Array of Integer: [1, 2, 3, 4, 5]
- Using Apache Commons toPrimitive(): Apache Commons Lang’s ArrayUtils class provides toPrimitive() method that can convert an array of object Integers to primitive ints. This set of integers needs to be converted to an array of Integers.
Algorithm:
- Get the set of integers
- Create an object of Primitive int by ArrayUtils.toPrimitive() method of Apache Commons Lang’s library
- Convert this primitive int to array of integer by use of toArray() method.
- Return/Print the created integer array int[]
Program:
// Java Program to convert// Set<Integer> to int[] in Java 8importorg.apache.commons.lang.ArrayUtils;importjava.util.*;importjava.util.stream.*;importjava.util.function.Function;classGFG {// Function to convert Set of Integer// to Set of Stringpublicstaticint[] convertIntSetToStringSet(Set<Integer> setOfInteger){returnArrayUtils.toPrimitive(setOfInteger.toArray(newInteger[0]));}publicstaticvoidmain(String args[]){// Create a set of integersSet<Integer> setOfInteger =newHashSet<>(Arrays.asList(1,2,3,4,5));// Print the set of IntegerSystem.out.println("Set of Integer: "+ setOfInteger);// Convert Set of integers to set of Stringint[] intArray = convertIntSetToStringSet(setOfInteger);// Print the set of StringSystem.out.println("Array of Integer: "+ Arrays.toString(intArray));}}Output:
Set of Integer: [1, 2, 3, 4, 5] Array of Integer: [1, 2, 3, 4, 5]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.



