Possible Duplicate:
Why does Guava's ImmutableList have so many overloaded of() methods?
Looking at Guava's ImmutableList (and some other classes), you'll find plenty of overloaded of
convenience methods ("Returns an immutable list containing the given elements, in order.") that take a different number of parameters:
...
public static <E> ImmutableList<E> of(E e1, E e2, E e3)
public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4)
public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5)
...
All the way to this one:
public static <E> ImmutableList<E> of(E e1,
E e2,
E e3,
E e4,
E e5,
E e6,
E e7,
E e8,
E e9,
E e10,
E e11,
E e12,
E... others)
Some colleagues of mine consider this silly, wondering why there's isn't just one method: of(E... elements)
. They suspect it's an ill-guided performance "optimisation" that falls into the category "do you think you're smarter than compiler", or something like that.
My hunch is that Kevin Bourrillion et al. put these methods in there for a real reason. Can anyone explain (or speculate) what that reason might be?
The comment in the source says:
// These go up to eleven. After that, you just get the varargs form, and
// whatever warnings might come along with it. :(
So, this was done because varargs methods produce a warning with generic arguments.
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