I\'ve got a HashSet
with a bunch of Integers
in it. I want to turn it into an array, but calling
hashset.toArray();
Try this. Using java 8.
Set<Integer> set = new HashSet<>();
set.add(43);
set.add(423);
set.add(11);
set.add(44);
set.add(56);
set.add(422);
set.add(34);
int[] arr = set.stream().mapToInt(Integer::intValue).toArray();
public int[] toInt(Set<Integer> set) {
int[] a = new int[set.size()];
int i = 0;
for (Integer val : set) a[i++] = val;
return a;
}
Now that I wrote the code for you it's not that manual anymore, is it? ;)