1

In Java 9, under the List interface, there is a new method of() which according to Java documentation:

The List.of() static factory methods provide a convenient way to create immutable lists.

They have overloaded of() with up to 10 arguments together with one vararg argument. What can be the rationale behind overloading of() with 10 arguments when they have provided one vararg argument overloaded method as well?enter image description here

jsm24
  • 39
  • 1
  • 6

1 Answers1

2

This is done for performance reasons.

First, empty, one element and two element lists are implemented as dedicated classes, which have no nested objects. So, constructing those is the fastest (only one allocation involved).

As for the higher "arities", the interface designers probably wanted to keep their options open to possibly implement more "fixed arity" list classes (though they had not followed this route just yet).

So indeed, the "higher arity" overloads get wrapped back into the "varargs" list constructor.

Reference: https://github.com/dmlloyd/openjdk/blob/7d7fbd09fcfd7f8cd02bf76ce10433ceeb33b3cf/jdk/src/java.base/share/classes/java/util/List.java#L788

oakad
  • 6,945
  • 1
  • 22
  • 31