1// First of all, you shouldn't use raw types.
2// Use :
3 Set<Integer> set = new HashSet<>();
4 set.add(4);
5 set.add(1);
6// Then convert to array :
7 Integer[] arr = x.toArray(new Integer[set.size()]);
8// Using x.toArray() would give you an Object[].