I've got a HashSet with a bunch of (you guessed it) integers in it. I want to turn it into an array, but calling

hashset.toArray();

returns an array of Object type. This is fine, but is there a better way to cast it to an array of int, other than iterating through every element manually? A method I want to pass it to

void doSomething(int[] arr)

Won't accept the Object[] array, even if I try casting it like

doSomething((int[]) hashSet.toArray());
share|improve this question

feedback

4 Answers

up vote 9 down vote accepted

Apache's ArrayUtils has this (it still iterates behind the scenes):

doSomething(ArrayUtils.toPrimitive(hashset.toArray()));

They're always a good place to check for things like this.

share|improve this answer
1  
Automatic +1 for recommending Apache Commons. – skaffman Mar 15 '10 at 23:41
@skaffman I'd recommend Apache Commons Lang ... not for this particular problem though ... but anyway, make your move ;) – sfussenegger Mar 15 '10 at 23:51
Actually, it doesn't iterate behind the scenes - it iterates twice. There is an unnecessary Integer[] that slows down the process of creating a primitive array from a collection. Actually, I think the choice to use Integer[] as parameter for toPrimitive(..) instead of Iterable<Integer> is a bit clunky as Arrays.asList(Integer[]) is a much faster operation than collection.toArray(). Hence I won't automatically +1 for recommending apache commons :) – sfussenegger Mar 16 '10 at 0:06
sfussenegger, Apache Commons (what I meant by "it") only iterates once. You're right that the creation of the array requires an iteration. – Matthew Flaschen Mar 16 '10 at 0:31
I though so but still wanted to emphasize it (the -1 wasn't from me btw) – sfussenegger Mar 16 '10 at 0:46
feedback

Nope; you've got to iterate over them. Sorry.

share|improve this answer
It makes sense if you think about it. ints aren't objects, so typecasting one to the other doesn't work. – Brendan Long Mar 15 '10 at 23:35
It seems odd that you can't get an array of ints from a HashSet of Integers though, shouldn't the autoboxing kick in and allow you to do it? It's not like the HashSet contains a mixture of types, they're all Integers so (as far as I can see), it wouldn't be a problem. – jackbot Mar 15 '10 at 23:46
1  
Collections can only hold objects (Integers in your case). Thus, it would be odd to have a special method to get an array of ints (corresponding methods would be required for other primitive types). Auto-boxing doesn't help, because that only applies for a single primitive<->object conversion. Keep in mind that the actual class (due to type erasure) is HashSet, not HashSet<Integer> (only a compile-time concept). – Matthew Flaschen Mar 16 '10 at 0:13
Makes perfect sense, thanks for explaining it :) – jackbot Mar 16 '10 at 0:23
feedback
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? ;)

share|improve this answer
1  
It wasn't that writing the code to do it manually was a problem, I was just curious to see if there was a better way to do it. – jackbot Mar 15 '10 at 23:44
Well, the essence of my answer was that the only way to avoid writing this code is getting others to write it (but actually that's the case for just about any code) - which you successfully did ;) – sfussenegger Mar 15 '10 at 23:48
Can you put null in a Set<Integer>? If so, this code will NPE. I'm not sure what the original poster would want to do with nulls though. I guess omit them from the resulting array. – davidsheldon Oct 14 '10 at 6:53
@davidsheldon Valid remark. You may put null into a Set<Integer> if the implementation supports it (obviously, yes, but that's what Set's Javadoc defines). HashSet and TreeMap will throw a NPE when trying to add null. Throwing a NPE in case set contains null seems to be the way to go though, of course along with appropriate documentation of this behavior - we all document thoroughly, don't we ;) – sfussenegger Oct 14 '10 at 11:21
feedback

You could also use the toArray(T[] contents) variant of the toArray() method. Create an empty array of ints of the same size as the HashSet, and then pass it to the toArray() method:

int[] myarray = new int[hashset.size()];
doSomething(hashset.toArray(myarray));
share|improve this answer
Nope, won't compile. – jkff Aug 7 at 2:38
It would help to provide your compile errors so I can tell what's wrong with my code. – Shiprack Aug 7 at 12:12
Why don't you just try compiling it yourself? Once you see the compile error, I can explain you why it happened. – jkff Aug 7 at 19:49
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.