Unlike sets, lists typically allow duplicate elements. More formally,
lists typically allow pairs of elements e1
and e2
such that e1.equals(e2)
, and they typically allow multiple
null elements if they allow null elements at all. It is not inconceivable
that someone might wish to implement a list that prohibits duplicates, by
throwing runtime exceptions when the user attempts to insert them, but we
expect this usage to be rare.
The List
interface places additional stipulations, beyond those
specified in the Collection
interface, on the contracts of the
iterator
, add
, remove
, equals
, and
hashCode
methods. Declarations for other inherited methods are
also included here for convenience.
The List
interface provides four methods for positional (indexed)
access to list elements. Lists (like Java arrays) are zero based. Note
that these operations may execute in time proportional to the index value
for some implementations (the LinkedList
class, for
example). Thus, iterating over the elements in a list is typically
preferable to indexing through it if the caller does not know the
implementation.
The List
interface provides a special iterator, called a
ListIterator
, that allows element insertion and replacement, and
bidirectional access in addition to the normal operations that the
Iterator
interface provides. A method is provided to obtain a
list iterator that starts at a specified position in the list.
The List
interface provides two methods to search for a specified
object. From a performance standpoint, these methods should be used with
caution. In many implementations they will perform costly linear
searches.
The List
interface provides two methods to efficiently insert and
remove multiple elements at an arbitrary point in the list.
Note
While it is permissible for lists to contain themselves as elements,
extreme caution is advised: the equals
and hashCode
methods are no longer well defined on such a list.
Some list implementations have restrictions on the elements that
they may contain. For example, some implementations prohibit null elements,
and some have restrictions on the types of their elements. Attempting to
add an ineligible element throws an unchecked exception, typically
NullPointerException
or ClassCastException
. Attempting
to query the presence of an ineligible element may throw an exception,
or it may simply return false; some implementations will exhibit the former
behavior and some will exhibit the latter. More generally, attempting an
operation on an ineligible element whose completion would not result in
the insertion of an ineligible element into the list may throw an
exception or it may succeed, at the option of the implementation.
Such exceptions are marked as "optional" in the specification for this
interface.
The List.
and
List.
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
.
subList
views implement the
RandomAccess
interface.
This interface is a member of the Java Collections Framework.
Collection
, Set
, ArrayList
, LinkedList
, Vector
, Arrays#asList(Object[])
, Collections#nCopies(int, Object)
, Collections#EMPTY_LIST
, AbstractList
, AbstractSequentialList
Modifier and Type | Method and Description |
---|---|
public boolean | add(E
element to be appended to this list e)Redeclares java. Appends the specified element to the end of this list (optional operation). |
public void | add(int
index at which the specified element is to be inserted index, E element to be inserted element)Inserts the specified element at the specified position in this list (optional operation). |
public boolean | Returns: true if this list changed as a result of the callcollection containing elements to be added to this list c)Redeclares java. Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). |
public boolean | Returns: true if this list changed as a result of the callindex at which to insert the first element from the
specified collection index, Collection<? extends E> collection containing elements to be added to this list c)Inserts all of the elements in the specified collection into this list at the specified position (optional operation). |
public default void | addFirst(E
the element to be added e)Overrides default java. Adds an element as the first element of this collection (optional operation). |
public default void | addLast(E
the element to be added. e)Overrides default java. Adds an element as the last element of this collection (optional operation). |
public void | clear()
Redeclares java. Removes all of the elements from this list (optional operation). |
public boolean | Returns: true if this list contains the specified elementelement whose presence in this list is to be tested o)Redeclares java. Returns |
public boolean | Returns: true if this list contains all of the elements of the
specified collectioncollection to be checked for containment in this list c)Redeclares java. Returns |
public static < the E> ListList 's element type | Returns: aList containing the elements of the given Collection a coll)Collection from which elements are drawn, must be non-nullReturns an unmodifiable List containing the elements of the given Collection, in its iteration order. |
public boolean | Returns: true if the specified object is equal to this listthe object to be compared for equality with this list o)Redeclares java. Compares the specified object with this list for equality. |
public E | Returns: the element at the specified position in this listindex of the element to return index)Returns the element at the specified position in this list. |
public default E | getFirst()
Overrides default java. Gets the first element of this collection. |
public default E | getLast()
Overrides default java. Gets the last element of this collection. |
public int | Returns: the hash code value for this listRedeclares java. Returns the hash code value for this list. |
public int | |
public boolean | Returns: true if this list contains no elementsRedeclares java. Returns |
public Iterator | Returns: an iterator over the elements in this list in proper sequenceRedeclares java. Returns an iterator over the elements in this list in proper sequence. |
public int | Returns: the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the elementelement to search for o)Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. |
public ListIterator | Returns: a list iterator over the elements in this list (in proper sequence)Returns a list iterator over the elements in this list (in proper sequence). |
public ListIterator | Returns: a list iterator over the elements in this list (in proper sequence), starting at the specified position in the listindex of the first element to be returned from the
list iterator (by a call to index)next )Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. |
public static < the E> ListList 's element type | |
public static < the E> ListList 's element type | Returns: aList containing the specified elementthe single element e1)Returns an unmodifiable list containing one element. |
public static < the E> ListList 's element type | Returns: aList containing the specified elementsthe first element e1, E the second element e2)Returns an unmodifiable list containing two elements. |
public static < the E> ListList 's element type | Returns: aList containing the specified elementsthe first element e1, E the second element e2, E the third element e3)Returns an unmodifiable list containing three elements. |
public static < the E> ListList 's element type | Returns: aList containing the specified elementsthe first element e1, E the second element e2, E the third element e3, E the fourth element e4)Returns an unmodifiable list containing four elements. |
public static < the E> ListList 's element type | Returns: aList containing the specified elementsthe first element e1, E the second element e2, E the third element e3, E the fourth element e4, E the fifth element e5)Returns an unmodifiable list containing five elements. |
public static < the E> ListList 's element type | Returns: aList containing the specified elementsthe first element e1, E the second element e2, E the third element e3, E the fourth element e4, E the fifth element e5, E the sixth element e6)Returns an unmodifiable list containing six elements. |
public static < the E> ListList 's element type | Returns: aList containing the specified elementsthe first element e1, E the second element e2, E the third element e3, E the fourth element e4, E the fifth element e5, E the sixth element e6, E the seventh element e7)Returns an unmodifiable list containing seven elements. |
public static < the E> ListList 's element type | Returns: aList containing the specified elementsthe first element e1, E the second element e2, E the third element e3, E the fourth element e4, E the fifth element e5, E the sixth element e6, E the seventh element e7, E the eighth element e8)Returns an unmodifiable list containing eight elements. |
public static < the E> ListList 's element type | Returns: aList containing the specified elementsthe first element e1, E the second element e2, E the third element e3, E the fourth element e4, E the fifth element e5, E the sixth element e6, E the seventh element e7, E the eighth element e8, E the ninth element e9)Returns an unmodifiable list containing nine elements. |
public static < the E> ListList 's element type | Returns: aList containing the specified elementsthe first element e1, E the second element e2, E the third element e3, E the fourth element e4, E the fifth element e5, E the sixth element e6, E the seventh element e7, E the eighth element e8, E the ninth element e9, E the tenth element e10)Returns an unmodifiable list containing ten elements. |
public static < the E> ListList 's element type | Returns: aList containing the specified elementsthe elements to be contained in the list elements)Returns an unmodifiable list containing an arbitrary number of elements. |
public boolean | Returns: true if this list contained the specified elementelement to be removed from this list, if present o)Redeclares java. Removes the first occurrence of the specified element from this list, if it is present (optional operation). |
public E | Returns: the element previously at the specified positionthe index of the element to be removed index)Removes the element at the specified position in this list (optional operation). |
public boolean | Returns: true if this list changed as a result of the callcollection containing elements to be removed from this list c)Redeclares java. Removes from this list all of its elements that are contained in the specified collection (optional operation). |
public default E | removeFirst()
Overrides default java. Removes and returns the first element of this collection (optional operation). |
public default E | removeLast()
Overrides default java. Removes and returns the last element of this collection (optional operation). |
public default void | replaceAll(UnaryOperator<E>
the operator to apply to each element operator)Replaces each element of this list with the result of applying the operator to that element (optional operation). |
public boolean | Returns: true if this list changed as a result of the callcollection containing elements to be retained in this list c)Redeclares java. Retains only the elements in this list that are contained in the specified collection (optional operation). |
public default List | Returns: a reverse-ordered view of this collection, as aList Implements java. Returns a reverse-ordered view of this collection. |
public E | Returns: the element previously at the specified positionindex of the element to replace index, E element to be stored at the specified position element)Replaces the element at the specified position in this list with the specified element (optional operation). |
public int | Returns: the number of elements in this listRedeclares java. Returns the number of elements in this list. |
public default void | sort(Comparator<? super E>
the c)Comparator used to compare list elements.
A null value indicates that the elements'
natural ordering should be usedSorts this list according to the order induced by the specified
|
public default Spliterator | Returns: aSpliterator over the elements in this listOverrides default java. Creates a |
public List | Returns: a view of the specified range within this listlow endpoint (inclusive) of the subList fromIndex, int high endpoint (exclusive) of the subList toIndex)Returns a view of the portion of this list between the specified
|
public Object[] | Returns: an array containing all of the elements in this list in proper sequenceRedeclares java. Returns an array containing all of the elements in this list in proper sequence (from first to last element). |
public <T> T[] | Returns: an array containing the elements of this listthe array into which the elements of this list are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose. a)Redeclares java. Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. |