views:

560

answers:

1

Since everyone praises Google Collections (e.g. in here) How come I can't find the equivalent of ArrayUtils.toObject() and ArrayUtils.toPrimitive()? is it that unusable? did I miss it?

+8  A: 

To be honest I'm not sure if either of those methods should even qualify as a collection-related operation and as such I'd wonder why they're even there in the first place.

To clarify a bit, collections are generically a group of objects with some semantic data binding them together while arrays are just a predetermined set of something. This semantic data may be information about accepting or rejecting nulls, duplicates, objects of wrong types or with unacceptable field values etc.

Most -if not all- collections do use arrays internally, however array itself isn't a collection. To qualify as a collection it needs some relevant magic such as removing and adding objects to arbitrary positions and arrays can't do that. I very much doubt you'll ever see any kind of array support in Google Collections since arrays are not collections.

However since Google Collections is going to be part of Google's Guava libraries which is a general purpose utility class library/framework of sorts, you may find what you want from com.google.common.primitives package, for example Booleans#asList(boolean... backingArray) and Booleans#toArray(Collection<Boolean> collection).

If you absolutely feel they should include equal methods to Apache Commons Collection's .toObject() and .toPrimitive() in there, you can always submit a feature request as new issue.

Esko
So you say ArrayUtils should have been in common-lang? I can agree with that, but most less keen programmers will still have looked in common-collections first... (yes, for some of us array is a collection) though I agree with the philosophical point of view
Ehrann Mehdan
Yeah. I know it's confusing if you're not really into collections and official definitions, but since I know Google's workers are into those things, I'd suspect that's why they've left array related operations (*mostly?*) out from Google Collections.
Esko
+1 from me. This is a good answer, well written.
duffymo
+1 from me as well, indeed, a very good answer, didn't expect it so quickly
Ehrann Mehdan
You got lucky with my "code compiling" (actually "graphics artist is making the final touches to layout code") moment :)
Esko
Thanks Esko for this great answer!Amusingly, we do have an ObjectArrays utility class in Google Collections, despite the true fact that arrays aren't collections. But still, the collections package has nothing primitives-related -- you have the correct references for those things.
Kevin Bourrillion