views:

718

answers:

4

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());
+2  A: 

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

Carl Manaster
It makes sense if you think about it. ints aren't objects, so typecasting one to the other doesn't work.
Brendan Long
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
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
Makes perfect sense, thanks for explaining it :)
jackbot
A: 

Have you seen this site?

Erik B
+1  A: 
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? ;)

sfussenegger
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
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
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
@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
+5  A: 

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.

Matthew Flaschen
Automatic +1 for recommending Apache Commons.
skaffman
@skaffman I'd recommend Apache Commons Lang ... not for this particular problem though ... but anyway, make your move ;)
sfussenegger
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
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
I though so but still wanted to emphasize it (the -1 wasn't from me btw)
sfussenegger