convert hashset to int array code example
Example 1: convert hashset to int array java
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
class Main
{
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));
}
}
Example 2: convert hashset to int java
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String args[]) {
Set<Integer> hashSet = new HashSet<Integer>();
hashSet.add(1124);
hashSet.add(3654);
hashSet.add(7854);
hashSet.add(9945);
System.out.println(hashSet);
Integer[] array = hashSet.stream().toArray(Integer[]::new);
System.out.println(Arrays.toString(array));
}
}