I am using the following code to convert a Set to int[]
Set<Integer> common = new HashSet<Integer>();
int[] myArray = (int[]) common.toArray();
the I got the following error:
error: incompatible types: Object[] cannot be converted to int[]
What would be the most clean way to do the conversion without adding element one by one using a for loop? Thanks!
You usually do this:
Set<Integer> common = new HashSet<Integer>();
int[] myArray = common.stream().mapToInt(Integer::intValue).toArray();
Set<Integer> common = new HashSet<>();
int[] values = Ints.toArray(common);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With