java util.List 接口上的多个静态工厂方法

Multiple static factory methods on java util.List interface

为什么 java util.List 接口中有多个 of 静态工厂方法取决于参数的数量,从 1 到 10?难道不可能只有一种方法将数组作为输入参数吗?

他们实现了 1 到 10 和 >10,因为 1 到 10 是常用的,并且比使用可变长度的 args 输入函数提供了很大的性能提升,例如 args...

我在 an article of Java World 中找到了答案,当你仔细想想时,这似乎很合乎逻辑。

In each method list, the first method creates an empty unmodifiable collection. The next 10 methods create unmodifiable collections with up to 10 elements. Despite their API clutter, these methods avoid the array allocation, initialization, and garbage collection overhead incurred by the final varargs method, which supports arbitrary-sized collections.