Top Description Methods
java.util

public Interface List<E>

extends SequencedCollection<E>
Known Direct Subinterfaces
java.lang.classfile.components.ClassPrinter.ListNode
Known Direct Implementers
java.util.ReverseOrderListView, java.util.Vector, java.util.concurrent.CopyOnWriteArrayList, java.util.concurrent.CopyOnWriteArrayList.COWSubList, java.util.concurrent.CopyOnWriteArrayList.Reversed, java.util.AbstractList, java.util.ArrayList, java.util.Collections.UnmodifiableList, java.util.Collections.SynchronizedList, java.util.Collections.CheckedList, java.util.ImmutableCollections.AbstractImmutableList, java.util.LinkedList
Type Parameters
<E>
the type of elements in this list
Imports
java.util.function.UnaryOperator

An ordered collection, where the user has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

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.

Unmodifiable Lists

The List.of and List.copyOf static factory methods provide a convenient way to create unmodifiable lists. The List instances created by these methods have the following characteristics:

This interface is a member of the Java Collections Framework.

Authors
Josh Bloch, Neal Gafter
Since
1.2
See Also
Collection, Set, ArrayList, LinkedList, Vector, Arrays#asList(Object[]), Collections#nCopies(int, Object), Collections#EMPTY_LIST, AbstractList, AbstractSequentialList

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true (as specified by Collection#add)
add
(E
element to be appended to this list
e
)

Redeclares java.util.Collection.add.

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 call
addAll
(Collection<? extends E>
collection containing elements to be added to this list
c
)

Redeclares java.util.Collection.addAll.

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 call
addAll
(int
index 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.util.SequencedCollection.addFirst.

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.util.SequencedCollection.addLast.

Adds an element as the last element of this collection (optional operation).

public void
clear()

Redeclares java.util.Collection.clear.

Removes all of the elements from this list (optional operation).

public boolean

Returns:

true if this list contains the specified element
contains
(Object
element whose presence in this list is to be tested
o
)

Redeclares java.util.Collection.contains.

Returns true if this list contains the specified element.

public boolean

Returns:

true if this list contains all of the elements of the specified collection
containsAll
(Collection<?>
collection to be checked for containment in this list
c
)

Redeclares java.util.Collection.containsAll.

Returns true if this list contains all of the elements of the specified collection.

public static <
the List's element type
E
>
List<E>

Returns:

a List containing the elements of the given Collection
copyOf
(Collection<? extends E>
a Collection from which elements are drawn, must be non-null
coll
)

Returns 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 list
equals
(Object
the object to be compared for equality with this list
o
)

Redeclares java.util.Collection.equals.

Compares the specified object with this list for equality.

public E

Returns:

the element at the specified position in this list
get
(int
index of the element to return
index
)

Returns the element at the specified position in this list.

public default E
getFirst()

Overrides default java.util.SequencedCollection.getFirst.

Gets the first element of this collection.

public default E
getLast()

Overrides default java.util.SequencedCollection.getLast.

Gets the last element of this collection.

public int

Returns:

the hash code value for this list
hashCode
()

Redeclares java.util.Collection.hashCode.

Returns the hash code value for this list.

public int

Returns:

the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element
indexOf
(Object
element to search for
o
)

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

public boolean

Returns:

true if this list contains no elements
isEmpty
()

Redeclares java.util.Collection.isEmpty.

Returns true if this list contains no elements.

public Iterator<E>

Returns:

an iterator over the elements in this list in proper sequence
iterator
()

Redeclares java.util.Collection.iterator.

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 element
lastIndexOf
(Object
element 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<E>

Returns:

a list iterator over the elements in this list (in proper sequence)
listIterator
()

Returns a list iterator over the elements in this list (in proper sequence).

public ListIterator<E>

Returns:

a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list
listIterator
(int
index of the first element to be returned from the list iterator (by a call to next)
index
)

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

public static <
the List's element type
E
>
List<E>

Returns:

an empty List
of
()

Returns an unmodifiable list containing zero elements.

public static <
the List's element type
E
>
List<E>

Returns:

a List containing the specified element
of
(E
the single element
e1
)

Returns an unmodifiable list containing one element.

public static <
the List's element type
E
>
List<E>

Returns:

a List containing the specified elements
of
(E
the first element
e1
,
E
the second element
e2
)

Returns an unmodifiable list containing two elements.

public static <
the List's element type
E
>
List<E>

Returns:

a List containing the specified elements
of
(E
the first element
e1
,
E
the second element
e2
,
E
the third element
e3
)

Returns an unmodifiable list containing three elements.

public static <
the List's element type
E
>
List<E>

Returns:

a List containing the specified elements
of
(E
the 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 List's element type
E
>
List<E>

Returns:

a List containing the specified elements
of
(E
the 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 List's element type
E
>
List<E>

Returns:

a List containing the specified elements
of
(E
the 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 List's element type
E
>
List<E>

Returns:

a List containing the specified elements
of
(E
the 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 List's element type
E
>
List<E>

Returns:

a List containing the specified elements
of
(E
the 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 List's element type
E
>
List<E>

Returns:

a List containing the specified elements
of
(E
the 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 List's element type
E
>
List<E>

Returns:

a List containing the specified elements
of
(E
the 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 List's element type
E
>
List<E>

Returns:

a List containing the specified elements
of
(E...
the 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 element
remove
(Object
element to be removed from this list, if present
o
)

Redeclares java.util.Collection.remove.

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 position
remove
(int
the 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 call
removeAll
(Collection<?>
collection containing elements to be removed from this list
c
)

Redeclares java.util.Collection.removeAll.

Removes from this list all of its elements that are contained in the specified collection (optional operation).

public default E
removeFirst()

Overrides default java.util.SequencedCollection.removeFirst.

Removes and returns the first element of this collection (optional operation).

public default E
removeLast()

Overrides default java.util.SequencedCollection.removeLast.

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 call
retainAll
(Collection<?>
collection containing elements to be retained in this list
c
)

Redeclares java.util.Collection.retainAll.

Retains only the elements in this list that are contained in the specified collection (optional operation).

public default List<E>

Returns:

a reverse-ordered view of this collection, as a List
reversed
()

Implements java.util.SequencedCollection.reversed.

Returns a reverse-ordered view of this collection.

public E

Returns:

the element previously at the specified position
set
(int
index 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 list
size
()

Redeclares java.util.Collection.size.

Returns the number of elements in this list.

public default void
sort(Comparator<? super E>
the Comparator used to compare list elements. A null value indicates that the elements' natural ordering should be used
c
)

Sorts this list according to the order induced by the specified Comparator (optional operation).

public default Spliterator<E>

Returns:

a Spliterator over the elements in this list
spliterator
()

Overrides default java.util.Collection.spliterator.

Creates a Spliterator over the elements in this list.

public List<E>

Returns:

a view of the specified range within this list
subList
(int
low 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 fromIndex, inclusive, and toIndex, exclusive.

public Object[]

Returns:

an array containing all of the elements in this list in proper sequence
toArray
()

Redeclares java.util.Collection.toArray.

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 list
toArray
(T[]
the 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.util.Collection.toArray.

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.