public final class Lists extends Object
List
interface
and, in addition, the JEP 269
"Unmodifiable List Static Factory Methods"
in the List
interface that were introduced in Java 9.
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.
|
static <E> void |
replaceAll(List<E> list,
UnaryOperator<E> operator)
Replaces each element of the passed list with the result of applying the
operator to that element.
|
static <E> void |
sort(List<E> list,
Comparator<? super E> c)
Sorts the passed list according to the order induced by the specified
Comparator . |
static <E> Spliterator<E> |
spliterator(List<E> list)
Creates a
Spliterator over the elements in the passed list. |
public static <E> void sort(List<E> list, Comparator<? super E> c)
Comparator
. The sort is stable: this method does not
reorder equal elements.
All elements in the list must be mutually comparable using the
specified comparator (that is, c.compare(e1, e2)
must not throw
a ClassCastException
for any elements e1
and e2
in the list).
If the specified comparator is null
then all elements in the
list must implement the Comparable
interface and the elements'
natural ordering should be used.
The list must be modifiable, but need not be resizable.
Implementation Requirements:
The implementation obtains an array containing all elements in
the list, sorts the array, and iterates over the list resetting each
element from the corresponding position in the array. (This avoids the
n2 log(n) performance that would result from attempting
to sort a linked list in place.)
E
- the type of the elements of the list to be sortedlist
- the list that should be sortedc
- the Comparator
used to compare list elements.
A null
value indicates that the elements'
natural ordering should be usedClassCastException
- if the list contains elements that are not
mutually comparable using the specified comparatorUnsupportedOperationException
- if the list's list-iterator does
not support the set
operationIllegalArgumentException
- (optional)
if the comparator is found to violate the Comparator
contractNullPointerException
- if the specified list is nullpublic static <E> void replaceAll(List<E> list, UnaryOperator<E> operator)
Implementation Requirements:
The default implementation is equivalent to, for the passed list
:
final ListIterator<E> li = list.listIterator();
while (li.hasNext()) {
li.set(operator.apply(li.next()));
}
If the list's list-iterator does not support the set
operation
then an UnsupportedOperationException
will be thrown when
replacing the first element.E
- the type of the elements of the list to be replacedlist
- the list whose elements should be replacedoperator
- the operator to apply to each elementUnsupportedOperationException
- if the passed list is unmodifiable.
Implementations may throw this exception if an element
cannot be replaced or if, in general, modification is not
supportedNullPointerException
- if the specified list is null or the specified
operator is null or if the operator result is a null value and the
passed list does not permit null elements (optional)public static <E> Spliterator<E> spliterator(List<E> list)
Spliterator
over the elements in the passed list.
The Spliterator
reports at least Spliterator.SIZED
,
Spliterator.ORDERED
and Spliterator.SUBSIZED
.
Implementation Note:
This implementation delegates to Spliterators.spliterator(java.util.Collection)
so it is effectively the same as calling
Spliterators.spliterator(list);
E
- the type of the elements of the list to be splittedlist
- the list to be splittedSpliterator
over the elements in the passed listpublic static <E> List<E> of()
E
- the List
's element typeList
public static <E> List<E> of(E e1)
E
- the List
's element typee1
- the single elementList
containing the specified elementNullPointerException
- if the element is null
public 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 null
public 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 null
public 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 null
public 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 null
public 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 null
public 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 null
public 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 null
public 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 null
public 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
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 null
public 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 Collection
NullPointerException
- if coll is null, or if it contains any nullsCopyright © 2018. All rights reserved.