- How to convert Integer Set to primitive int array.
- By Using java 8 Streams we can convert Set to array.
- set.stream().mapToInt(Number::intValue).toArray();
- Lets see an example java program on how to convert integer set to int array using java 8
#1: Java Example program on converting Integer Set to int Array
- package com.instanceofjava;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.Set;
- public class SetToArray {
- /**
- * @author www.Instanceofjava.com
- * @category interview programming questions
- *
- * Description: convert Integer set to int array using java 8
- *
- */
- public static void main(String[] args) {
- Set
hashset= new HashSet(Arrays.asList(12,34,56,78,99)); - int[] array = hashset.stream().mapToInt(Number::intValue).toArray();
- for (int i : array) {
- System.out.println(i);
- }
- }
- }
Output:
- 34
- 99
- 56
- 12
- 78