What is the point of overloaded Convenience Factory Methods for Collections in Java 9
Java 9 comes with convenience factory methods for creating immutable lists. Finally a list creation is as simple as:
List<String> list = List.of("foo", "bar");
But there are 12 overloaded versions of this method, 11 with 0 to 10 elements, and one with var args.
static <E> List<E> of(E... elements)
Same is the case with Set
and Map
.
Since there is a var args method, what is the point of having extra 11 methods?
What I think is that var-args create an array, so the other 11 methods can skip creation of an extra object and in most cases 0 - 10 elements will do. Is there any other reason for this?
From the JEP docs itself -
Description -
These will include varargs overloads, so that there is no fixed limit on the collection size. However, the collection instances so created may be tuned for smaller sizes. Special-case APIs (fixed-argument overloads) for up to ten of elements will be provided. While this introduces some clutter in the API, it avoids array allocation, initialization, and garbage collection overhead that is incurred by varargs calls. Significantly, the source code of the call site is the same regardless of whether a fixed-arg or varargs overload is called.
Edit - To add motivation and as already mentioned in the comments by @CKing too :
Non-Goals -
It is not a goal to support high-performance, scalable collections with arbitrary numbers of elements. The focus is on small collections .
Motivation -
Creating a small, unmodifiable collection (say, a set) involves constructing it, storing it in a local variable, and invoking add() on it several times, and then wrapping it.
Set<String> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("a", "b", "c")));
The Java 8 Stream API can be used to construct small collections, by combining stream factory methods and collectors.
// Java 8
Set<String> set1 = Collections.unmodifiableSet(Stream.of("a", "b", "c").collect(Collectors.toSet()));
Much of the benefit of collection literals can be gained by providing library APIs for creating small collection instances, at significantly reduced cost and risk compared to changing the language. For example, the code to create a small Set instance might look like this:
// Java 9
Set set2 = Set.of("a", "b", "c");
As you suspected, this is a performance enhancement. Vararg methods create an array "under the hood", and having method which take 1-10 arguments directly avoids this redundant array creation.
You may find the following passage of item 42 of Josh Bloch's Effective Java (2nd ed.) enlightening:
Every invocation of a varargs method causes an array allocation and initialization. If you have determined empirically that you can't afford this cost but you need the flexibility of varargs, there is a pattern that lets you have your cake and eat it too. Suppose you've determined that 95 percent of the calls to a method have three or fewer parameters. Then declare five overloadings of the method, one each with zero through three ordinary parameters, and a single varargs method for use when the number of arguments exceeds three [...]
链接地址: http://www.djcxy.com/p/27726.html上一篇: 如何初始化一个数组的所有成员为相同的值?