Equivalent overload method, why necessary?

I looked at the JAVA code made by Google and I found ImmutableSet: http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ImmutableSet.html

They implemented method () in several ways:

public static <E> ImmutableSet<E> of(E e1, E e2); public static <E> ImmutableSet<E> of(E e1, E e2, E e3); public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4); public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5); public static <E> ImmutableSet<E> of(E... elements); 

I checked the implementation, which is here: https://code.google.com/p/google-collections/source/browse/trunk/src/com/google/common/collect/ImmutableSet.java

There a method is created with the following signature:

 private static <E> ImmutableSet<E> create(E... elements) 

which wraps

 private static <E> ImmutableSet<E> create(Iterable<? extends E> iterable, int count); 

method. Public methods simply pass parameters to the create (E ... elements) method, which ultimately calls another create method.

I assume that the public methods with a fixed parameter value is not needed, since we have a method (E ... elements).

My question is why did they do this? Representation? Or is it a sample?

Thanks.

+8
java performance theory
source share
3 answers

This cannot be related to performance, in fact: all methods are delegated to the same creation method that the array expects anyway.

I guess this is due to warnings. Consider the following minimal snippet:

 import java.util.List; class ImmutableSet<T> { } public class ParametersTest { public static void main(String[] args) { List<String> list0 = null; List<String> list1 = null; of(list0, list1); } @SuppressWarnings("unchecked") public static <E> ImmutableSet<E> of(E e1, E e2) { return create(e1, e2); } public static <E> ImmutableSet<E> of(E... elements) { return create(elements); } private static <E> ImmutableSet<E> create(E... elements) { return null; } } 

The call of of in the main method is in order: it corresponds to the 2-args version of the of method. Now comment out the 2-args version of the of method. Then the call is still fine, but will directly invoke the varags version. This will create a shared array and trigger a warning. (This warning is suppressed in version 2-args, obviously).

So, to summarize, I assume that this is necessary to avoid warnings for library clients who want to call the of method with several objects of a generic type.

Fortunately, such things will no longer be needed in the future thanks to http://docs.oracle.com/javase/7/docs/api/java/lang/SafeVarargs.html

+4
source share

Performance. To call the E ... version of the method, the caller must allocate a new array. The caller only needs to push the argi on the stack to invoke other methods.

+2
source share

They do this to manage memory in a more efficient way. If you have an immutable collection with a small set of elements, it is better to explicitly set the size of the collection. Otherwise, Java will create a larger collection. For example: HashSet, unless otherwise specified, will be 12 entries in size.

+1
source share

All Articles