Enhanced text request on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 334
Posted: Tue Mar 03, 2020 11:21 am
Correctly states that all the artifacts List,Set,Map in Java 9 was added methods for creating unmodified collections for 0..10 parameters but it doesn't state that a handy varargs methods is also present when you need to create a collection with more than 10 parameters.
It would be great if this var-args method is added.
Code: Select all
static <E> List<E> of(E... elements) {
switch (elements.length) { // implicit null check of elements
case 0:
return ImmutableCollections.emptyList();
case 1:
return new ImmutableCollections.List12<>(elements[0]);
case 2:
return new ImmutableCollections.List12<>(elements[0], elements[1]);
default:
return new ImmutableCollections.ListN<>(elements);
}
}