kucci / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Suggestion: Provide methods to work with Object[] #406

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
There are several useful methods such as join and indexOf for primitive arrays 
(in com.google.common.primitives.*), but no such methods exist for object 
arrays (neither in com.google.common.base.Objects nor in 
com.google.common.collect.ObjectArrays).

Original issue reported on code.google.com by eric.j...@gmail.com on 24 Aug 2010 at 6:43

GoogleCodeExporter commented 9 years ago
For join, Joiner works on object arrays. For indexOf, you can just do 
Arrays.asList(array).indexOf(obj). Arrays.asList allows you to use most 
collection APIs with arrays.

Original comment by cgdec...@gmail.com on 24 Aug 2010 at 8:39

GoogleCodeExporter commented 9 years ago
Makes sense, but then I wonder what the point of e.g. Ints.indexOf(int) is if 
Ints.asList(int[]).indexOf(int) is just as good?

Original comment by eric.j...@gmail.com on 24 Aug 2010 at 9:37

GoogleCodeExporter commented 9 years ago
The indexOf method on the list returned by Ints.asList does seem to be 
optimized to minimize boxing of values, but if you call it with an int, that 
int is going to be boxed into an Integer and then unboxed again. No boxing will 
happen with Ints.indexOf. The boxing issue won't exist with object arrays.

I'd say the main reason most of the methods in the primitives package exist is 
that, unlike with objects, there are actual reasons to use arrays of primitives 
rather than collections. For objects, collections should pretty much always be 
used rather than arrays.

Original comment by cgdec...@gmail.com on 24 Aug 2010 at 10:35

GoogleCodeExporter commented 9 years ago
Thanks for the clarification.

Original comment by eric.j...@gmail.com on 24 Aug 2010 at 10:39

GoogleCodeExporter commented 9 years ago
cgdecker is right.  We are averse to adding more support for Object arrays, 
because they are so inferior to collections in so many ways it's ridiculous.  
Unless you are a collection implementation yourself, you should probably never 
use them.

Original comment by kevinb@google.com on 8 Sep 2010 at 6:05

GoogleCodeExporter commented 9 years ago
Amen kevinb! Maybe you can hit up Mr. Bloch to add this as an item in the 3rd 
edition of Effective Java. Arrays are an implementation detail, and not 
suitable for any public API.

Original comment by ray.j.gr...@gmail.com on 13 Sep 2010 at 6:06

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09