Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么 Java 9 的 List.of 有这么多个重载? #312

Open
Bpazy opened this issue Nov 7, 2023 · 0 comments
Open

为什么 Java 9 的 List.of 有这么多个重载? #312

Bpazy opened this issue Nov 7, 2023 · 0 comments

Comments

@Bpazy
Copy link
Owner

Bpazy commented Nov 7, 2023

代码如下:

     static <E> List<E> of() {
         return (List<E>) ImmutableCollections.EMPTY_LIST;
     }
 
     static <E> List<E> of(E e1) {
         return new ImmutableCollections.List12<>(e1);
     }
 
     static <E> List<E> of(E e1, E e2) {
         return new ImmutableCollections.List12<>(e1, e2);
     }
 
     static <E> List<E> of(E e1, E e2, E e3) {
         return ImmutableCollections.listFromTrustedArray(e1, e2, e3);
     }
 
     static <E> List<E> of(E e1, E e2, E e3, E e4) {
         return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4);
     }
 
     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) {
         return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4, e5);
     }
 
     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
         return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4, e5,
                                                          e6);
     }
 
     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
         return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4, e5,
                                                          e6, e7);
     }
 
     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
         return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4, e5,
                                                          e6, e7, e8);
     }
 
     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
         return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4, e5,
                                                          e6, e7, e8, e9);
     }
 
     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
         return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4, e5,
                                                          e6, e7, e8, e9, e10);
     }
 
     static <E> List<E> of(E... elements) {
         switch (elements.length) { // implicit null check of elements
             case 0:
                 @SuppressWarnings("unchecked")
                 var list = (List<E>) ImmutableCollections.EMPTY_LIST;
                 return list;
             case 1:
                 return new ImmutableCollections.List12<>(elements[0]);
             case 2:
                 return new ImmutableCollections.List12<>(elements[0], elements[1]);
             default:
                 return ImmutableCollections.listFromArray(elements);
         }
     }
 

Java 提案里有提到这个: https://openjdk.org/jeps/269

其中关键的一段:

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.

翻译一下:
这些将包括可变参数重载,因此集合大小没有固定限制。然而,如此创建的集合实例可以调整为更小的尺寸。将提供最多十个元素的特殊情况 API(固定参数重载)。虽然这会给 API 带来一些混乱,但它避免了由 varargs 调用引起的数组分配、初始化和垃圾收集开销。值得注意的是,无论调用的是固定参数重载还是可变参数重载,调用站点的源代码都是相同的。

结论: 虽然这种做法让 API 有点混乱,但避免了由 varargs 调用引起的数组分配、初始化和垃圾收集开销。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant