Convert Set of Integer to array of int in Java
This post will discuss how to convert a set of Integer to a primitive integer array in Java.
1. Using Java 8
We can use the Stream provided by Java 8 to convert a Set<Integer> to a primitive integer array. Following are the complete steps with an explanation:
- Convert given
Set<Integer>toStream<Integer>usingSet.stream()method. - Convert
Stream<Integer>toIntStream.
We can’t directly convert Stream<Integer> to int[] as Stream.toArray() returns an Object[] and Stream.toArray(IntFunction<A[]>) returns a generic A[]. Fortunately, we have IntStream that can handle primitive int, whose toArray() method returns int[].
Now the problem reduces to how to convert Stream<Integer> to IntStream. There are many ways to map Integer to their primitive form:
mapToInt(Integer::intValue) // method reference
mapToInt(i -> i.intValue()) // Explicit unboxing with a lambda expression
mapToInt(i -> i) // Implicit auto-unboxing with a lambda expression
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.Set; import java.util.HashSet; class Main { // Program to convert a `Set<Integer>` to a primitive integer array in Java public static void main(String[] args) { Set<Integer> ints = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); int[] primitive = ints.stream() .mapToInt(Integer::intValue) .toArray(); System.out.println(Arrays.toString(primitive)); } } |
Output:
[1, 2, 3, 4, 5]
IntStream.toArray() throws a NullPointerException if any null values are present in the set. We can easily filter out the null values before mapping:
|
1 2 3 4 |
int[] primitive = set.stream() .filter(Objects::nonNull) .mapToInt(Integer::intValue) .toArray(); |
2. Using Guava Library
Guava’s Ints.toArray() can also convert a Set<Integer> to a primitive integer array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import com.google.common.primitives.Ints; import java.util.Arrays; import java.util.Set; import java.util.HashSet; class Main { // Program to convert a `Set<Integer>` to a primitive integer array using Guava public static void main(String[] args) { Set<Integer> ints = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); int[] primitive = Ints.toArray(ints); System.out.println(Arrays.toString(primitive)); } } |
Output:
[1, 2, 3, 4, 5]
Ints.toArray() throws a NullPointerException if any null values are present in the set. We can filter null values before passing it to Ints.toArray() as seen before.
|
1 2 3 |
int[] primitive = Ints.toArray(ints.stream() .filter(Objects::nonNull) .collect(Collectors.toSet())); |
3. Using Apache Commons Lang
Apache Commons Lang’s ArrayUtils class provides a toPrimitive() method that can convert an Integer object array to array of primitive ints. We need to convert a Set<Integer> to a primitive integer array first. We can use Set.toArray() for easy conversion.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; import java.util.Set; import java.util.HashSet; class Main { // Program to convert a `Set<Integer>` to a primitive integer array in Java public static void main(String[] args) { Set<Integer> ints = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); int[] primitive = ArrayUtils.toPrimitive(ints.toArray(new Integer[0])); System.out.println(Arrays.toString(primitive)); } } |
toPrimitive() throws a NullPointerException if any null values are present in the set. We can filter null values before passing them to the function, as shown below:
|
1 2 |
Integer[] boxed = ints.stream().filter(Objects::nonNull).toArray(Integer[]::new); int[] primitive = ArrayUtils.toPrimitive(boxed); |
That’s all about converting Set of Integer to an array of int in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)