Guava's ImmutableList
has a series of overloaded of()
methods. As discussed in the context of this solved question, these exist to avoid the warnings that occur when mixing varargs with generics.
But in addition to that, the 0 and 1 parameter methods each rely on a specialized list implementation. It would seem that the same thing could be done for the 2..11 parameter methods, thereby reducing memory consumption of these lists - along the lines of
final class ImmutableListWith2Elements<E> extends ImmutableList<E> {
final E e1;
final E e2;
...
Instead, they use an array-based implementation, which means storing an array object and a reference to the array in addition to the content references. Can you help me understand the trade-offs involved here?