public final class Lists extends Object
List interface specified in
JEP 269
"Unmodifiable List Static Factory Methods".
The Lists.of and Lists.copyOf static factory methods provide a convenient way to create
unmodifiable lists. The List instances created by these methods have
the following characteristics:
UnsupportedOperationException to be thrown.
However, if the contained elements are themselves mutable,
this may cause the List's contents to appear to change.
null elements. Attempts to create them with
null elements result in NullPointerException.
==), identity hash code, and synchronization) are
unreliable and should be avoided.
| Modifier and Type | Method and Description |
|---|---|
static <E> List<E> |
copyOf(Collection<? extends E> coll)
Returns an unmodifiable List containing the elements of
the given Collection, in its iteration order.
|
static <E> List<E> |
of()
Returns an unmodifiable list containing zero elements.
|
static <E> List<E> |
of(E... elements)
Returns an unmodifiable list containing an arbitrary number of elements.
|
static <E> List<E> |
of(E e1)
Returns an unmodifiable list containing one element.
|
static <E> List<E> |
of(E e1,
E e2)
Returns an unmodifiable list containing two elements.
|
static <E> List<E> |
of(E e1,
E e2,
E e3)
Returns an unmodifiable list containing three elements.
|
static <E> List<E> |
of(E e1,
E e2,
E e3,
E e4)
Returns an unmodifiable list containing four elements.
|
static <E> List<E> |
of(E e1,
E e2,
E e3,
E e4,
E e5)
Returns an unmodifiable list containing five elements.
|
static <E> List<E> |
of(E e1,
E e2,
E e3,
E e4,
E e5,
E e6)
Returns an unmodifiable 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 unmodifiable 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 unmodifiable 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 unmodifiable 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 unmodifiable list containing ten elements.
|
public static <E> List<E> of()
E - the List's element typeListpublic static <E> List<E> of(E e1)
E - the List's element typee1 - the single elementList containing the specified elementNullPointerException - if the element is nullpublic static <E> List<E> of(E e1, E e2)
E - the List's element typee1 - the first elemente2 - the second elementList containing the specified elementsNullPointerException - if an element is nullpublic static <E> List<E> of(E e1, E e2, E e3)
E - the List's element typee1 - the first elemente2 - the second elemente3 - the third elementList containing the specified elementsNullPointerException - if an element is nullpublic static <E> List<E> of(E e1, E e2, E e3, E e4)
E - the List's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elementList containing the specified elementsNullPointerException - if an element is nullpublic static <E> List<E> of(E e1, E e2, E e3, E e4, E e5)
E - the List's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elemente5 - the fifth elementList containing the specified elementsNullPointerException - if an element is nullpublic static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6)
E - the List's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elemente5 - the fifth elemente6 - the sixth elementList containing the specified elementsNullPointerException - if an element is nullpublic static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
E - the List's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elemente5 - the fifth elemente6 - the sixth elemente7 - the seventh elementList containing the specified elementsNullPointerException - if an element is nullpublic static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
E - the List's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elemente5 - the fifth elemente6 - the sixth elemente7 - the seventh elemente8 - the eighth elementList containing the specified elementsNullPointerException - if an element is nullpublic static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
E - the List's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elemente5 - the fifth elemente6 - the sixth elemente7 - the seventh elemente8 - the eighth elemente9 - the ninth elementList containing the specified elementsNullPointerException - if an element is nullpublic 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)
E - the List's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elemente5 - the fifth elemente6 - the sixth elemente7 - the seventh elemente8 - the eighth elemente9 - the ninth elemente10 - the tenth elementList containing the specified elementsNullPointerException - if an element is null@SafeVarargs public static <E> List<E> of(E... elements)
API Note:
This method also accepts a single array as an argument. The element type of
the resulting list will be the component type of the array, and the size of
the list will be equal to the length of the array. To create a list with
a single element that is an array, do the following:
String[] array = ... ;
List<String[]> list = Lists.<String[]>of(array);
This will cause the Lists.of(E) method
to be invoked instead.E - the List's element typeelements - the elements to be contained in the listList containing the specified elementsNullPointerException - if an element is null or if the array is nullpublic static <E> List<E> copyOf(Collection<? extends E> coll)
Implementation Note: If the given Collection is an unmodifiable List, calling copyOf will generally not create a copy.
E - the List's element typecoll - the collection from which elements are drawn, must be non-nullList containing the elements of the given CollectionNullPointerException - if coll is null, or if it contains any nullsCopyright © 2020. All rights reserved.