• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Point of static of methods of List API

 
Greenhorn
Posts: 13
  • 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: 1275
39
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: 70224
282
  • 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: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys.
 
catch it before it slithers away! Oh wait, it's a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic