1import java.util.HashSet;
2import java.util.Set;
3public class SetExample {
4 public static void main(String args[]) {
5 //Instantiating the HashSet
6 Set<Integer> hashSet = new HashSet<Integer>();
7 //Populating the HashSet
8 hashSet.add(1124);
9 hashSet.add(3654);
10 hashSet.add(7854);
11 hashSet.add(9945);
12 //Creating an empty integer array
13 Integer[] array = new Integer[hashSet.size()];
14 //Converting Set object to integer array
15 hashSet.toArray(array);
16 System.out.println(Arrays.toString(array));
17 }
18}
1import java.util.Arrays;
2import java.util.HashSet;
3import java.util.Set;
4public class SetExample {
5 public static void main(String args[]) {
6 //Instantiating the HashSet
7 Set<Integer> hashSet = new HashSet<Integer>();
8 //Populating the HashSet
9 hashSet.add(1124);
10 hashSet.add(3654);
11 hashSet.add(7854);
12 hashSet.add(9945);
13 System.out.println(hashSet);
14 //Creating an empty integer array
15 Integer[] array = hashSet.stream().toArray(Integer[]::new);
16 System.out.println(Arrays.toString(array));
17 }
18}