This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Point of static of methods of List API

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API:

https://docs.oracle.com/javase/9/docs/api/java/util/List.html


static <E> List<E> of​()
Returns an immutable list containing zero elements.
static <E> List<E> of​(E e1)
Returns an immutable list containing one element.
static <E> List<E> of​(E... elements)
Returns an immutable list containing an arbitrary number of elements.
static <E> List<E> of​(E e1, E e2)
Returns an immutable list containing two elements.
static <E> List<E> of​(E e1, E e2, E e3)
Returns an immutable list containing three elements.
static <E> List<E> of​(E e1, E e2, E e3, E e4)
Returns an immutable list containing four elements.
static <E> List<E> of​(E e1, E e2, E e3, E e4, E e5)
Returns an immutable list containing five elements.
static <E> List<E> of​(E e1, E e2, E e3, E e4, E e5, E e6)
Returns an immutable list containing six elements.
static <E> List<E> of​(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
Returns an immutable list containing seven elements.
static <E> List<E> of​(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
Returns an immutable list containing eight elements.
static <E> List<E> of​(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
Returns an immutable list containing nine elements.
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)
Returns an immutable list containing ten elements.



My question is: what's the point of having the methods like for 1, 2, ... 10 elements if there is the varargs method which takes any number of parameters anyway?


static <E> List<E> of​(E... elements)
Returns an immutable list containing an arbitrary number of elements.

 
Bartender
Posts: 1175
38
IBM DB2 Netbeans IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember well, is mainly due to performance reasons (internally a finite number of items can be handled more efficiently than varargs)
 
Marshal
Posts: 65770
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claude is correct. It allows the compiler to link the method call to the 9‑args method (for example, if you have nine arguments), which is much faster than creating a 9‑element array at runtime which is only used for that method call.
 
Ahmed Sabrioglu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys.
 
A berm makes a great wind break. And Iwe all like to break wind once in a while. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!