convert hashset to int array java

Solutions on MaxInterview for convert hashset to int array java by the best coders in the world

showing results for - "convert hashset to int array java"
Jessim
19 Jul 2020
1import java.util.Arrays;
2import java.util.Set;
3import java.util.HashSet;
4class Main
5{
6    // Program to convert set of integer to array of int in Java
7    public static void main(String args[])
8    {
9        Set<Integer> ints = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));
10        int[] primitive = ints.stream()
11                            .mapToInt(Integer::intValue)
12                            .toArray();
13        System.out.println(Arrays.toString(primitive));
14    }
15}
Louie
08 Jan 2021
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}
Nael
19 Sep 2020
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}