public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Queue<E>, Cloneable, Serializable
List, backed by a doubly-linked list.
All optional operations including adding, removing, and replacing elements are supported.
All elements are permitted, including null.
This class is primarily useful if you need queue-like behavior. It may also be useful
as a list if you expect your lists to contain zero or one element, but still require the
ability to scale to slightly larger numbers of elements. In general, though, you should
probably use ArrayList if you don't need the queue-like behavior.
modCount| Constructor and Description |
|---|
LinkedList()
Constructs a new empty instance of
LinkedList. |
LinkedList(Collection<? extends E> collection)
Constructs a new instance of
LinkedList that holds all of the
elements contained in the specified collection. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E object)
Adds the specified object at the end of this
LinkedList. |
void |
add(int location,
E object)
Inserts the specified object into this
LinkedList at the
specified location. |
boolean |
addAll(Collection<? extends E> collection)
Adds the objects in the specified Collection to this
LinkedList. |
boolean |
addAll(int location,
Collection<? extends E> collection)
Inserts the objects in the specified collection at the specified location
in this
LinkedList. |
void |
addFirst(E object)
Adds the specified object at the beginning of this
LinkedList. |
void |
addLast(E object)
Adds the specified object at the end of this
LinkedList. |
void |
clear()
Removes all elements from this
LinkedList, leaving it empty. |
Object |
clone()
Returns a new
LinkedList with the same elements and size as this
LinkedList. |
boolean |
contains(Object object)
Searches this
LinkedList for the specified object. |
Iterator<E> |
descendingIterator()
Returns an iterator over the elements in this deque in reverse
sequential order.
|
E |
element()
Retrieves, but does not remove, the head of the queue represented by
this deque (in other words, the first element of this deque).
|
E |
get(int location)
Returns the element at the specified location in this list.
|
E |
getFirst()
Returns the first element in this
LinkedList. |
E |
getLast()
Returns the last element in this
LinkedList. |
int |
indexOf(Object object)
Searches this list for the specified object and returns the index of the
first occurrence.
|
int |
lastIndexOf(Object object)
Searches this
LinkedList for the specified object and returns the
index of the last occurrence. |
ListIterator<E> |
listIterator(int location)
Returns a ListIterator on the elements of this
LinkedList. |
boolean |
offer(E o)
Inserts the specified element into the queue represented by this deque
(in other words, at the tail of this deque) if it is possible to do so
immediately without violating capacity restrictions, returning
true upon success and false if no space is currently
available. |
boolean |
offerFirst(E e)
Inserts the specified element at the front of this deque unless it would
violate capacity restrictions.
|
boolean |
offerLast(E e)
Inserts the specified element at the end of this deque unless it would
violate capacity restrictions.
|
E |
peek()
Retrieves, but does not remove, the head of the queue represented by
this deque (in other words, the first element of this deque), or
returns
null if this deque is empty. |
E |
peekFirst()
Retrieves, but does not remove, the first element of this deque,
or returns
null if this deque is empty. |
E |
peekLast()
Retrieves, but does not remove, the last element of this deque,
or returns
null if this deque is empty. |
E |
poll()
Retrieves and removes the head of the queue represented by this deque
(in other words, the first element of this deque), or returns
null if this deque is empty. |
E |
pollFirst()
Retrieves and removes the first element of this deque,
or returns
null if this deque is empty. |
E |
pollLast()
Retrieves and removes the last element of this deque,
or returns
null if this deque is empty. |
E |
pop()
Pops an element from the stack represented by this deque.
|
void |
push(E e)
Pushes an element onto the stack represented by this deque (in other
words, at the head of this deque) if it is possible to do so
immediately without violating capacity restrictions, throwing an
IllegalStateException if no space is currently available. |
E |
remove()
Retrieves and removes the head of the queue represented by this deque
(in other words, the first element of this deque).
|
E |
remove(int location)
Removes the object at the specified location from this
LinkedList. |
boolean |
remove(Object object)
Removes one instance of the specified object from this
Collection if one
is contained (optional). |
E |
removeFirst()
Removes the first object from this
LinkedList. |
boolean |
removeFirstOccurrence(Object o)
Removes the first occurrence of the specified element from this deque.
|
E |
removeLast()
Removes the last object from this
LinkedList. |
boolean |
removeLastOccurrence(Object o)
Removes the last occurrence of the specified element from this deque.
|
E |
set(int location,
E object)
Replaces the element at the specified location in this
LinkedList
with the specified object. |
int |
size()
Returns the number of elements in this
LinkedList. |
Object[] |
toArray()
Returns a new array containing all elements contained in this
LinkedList. |
<T> T[] |
toArray(T[] contents)
Returns an array containing all elements contained in this
LinkedList. |
iteratorequals, hashCode, listIterator, removeRange, subListcontainsAll, isEmpty, removeAll, retainAll, toStringfinalize, getClass, notify, notifyAll, wait, wait, waitcontainsAll, copyOf, equals, hashCode, isEmpty, iterator, listIterator, of, of, of, of, of, of, of, of, of, of, of, of, removeAll, replaceAll, retainAll, sort, spliterator, subListforEach, parallelStream, removeIf, stream, toArraypublic LinkedList()
LinkedList.public LinkedList(Collection<? extends E> collection)
LinkedList that holds all of the
elements contained in the specified collection. The order of the
elements in this new LinkedList will be determined by the
iteration order of collection.collection - the collection of elements to add.public void add(int location,
E object)
LinkedList at the
specified location. The object is inserted before any previous element at
the specified location. If the location is equal to the size of this
LinkedList, the object is added at the end.add in interface List<E>add in class AbstractSequentialList<E>location - the index at which to insert.object - the object to add.IndexOutOfBoundsException - if location < 0 || location > size()public boolean add(E object)
LinkedList.public boolean addAll(int location,
Collection<? extends E> collection)
LinkedList. The objects are added in the order they are
returned from the collection's iterator.addAll in interface List<E>addAll in class AbstractSequentialList<E>location - the index at which to insert.collection - the collection of objectstrue if this LinkedList is modified,
false otherwise.ClassCastException - if the class of an object is inappropriate for this list.IllegalArgumentException - if an object cannot be added to this list.IndexOutOfBoundsException - if location < 0 || location > size()public boolean addAll(Collection<? extends E> collection)
LinkedList.addAll in interface Collection<E>addAll in interface Deque<E>addAll in interface List<E>addAll in class AbstractCollection<E>collection - the collection of objects.true if this LinkedList is modified,
false otherwise.Collection.add(Object)public void addFirst(E object)
LinkedList.public void addLast(E object)
LinkedList.public void clear()
LinkedList, leaving it empty.clear in interface Collection<E>clear in interface List<E>clear in class AbstractList<E>List.isEmpty(),
sizepublic Object clone()
LinkedList with the same elements and size as this
LinkedList.public boolean contains(Object object)
LinkedList for the specified object.contains in interface Collection<E>contains in interface Deque<E>contains in interface List<E>contains in class AbstractCollection<E>object - the object to search for.true if object is an element of this
LinkedList, false otherwisepublic E get(int location)
AbstractListpublic E getFirst()
LinkedList.getFirst in interface Deque<E>NoSuchElementException - if this LinkedList is empty.public E getLast()
LinkedList.getLast in interface Deque<E>NoSuchElementException - if this LinkedList is emptypublic int indexOf(Object object)
AbstractListpublic int lastIndexOf(Object object)
LinkedList for the specified object and returns the
index of the last occurrence.lastIndexOf in interface List<E>lastIndexOf in class AbstractList<E>object - the object to search forpublic ListIterator<E> listIterator(int location)
LinkedList. The
elements are iterated in the same order that they occur in the
LinkedList. The iteration starts at the specified location.listIterator in interface List<E>listIterator in class AbstractSequentialList<E>location - the index at which to start the iterationLinkedListIndexOutOfBoundsException - if location < 0 || location > size()ListIteratorpublic E remove(int location)
LinkedList.remove in interface List<E>remove in class AbstractSequentialList<E>location - the index of the object to removeIndexOutOfBoundsException - if location < 0 || location >= size()public boolean remove(Object object)
AbstractCollectionCollection if one
is contained (optional). This implementation iterates over this
Collection and tests for each element e returned by the iterator,
whether e is equal to the given object. If object != null
then this test is performed using object.equals(e), otherwise
using object == null. If an element equal to the given object is
found, then the remove method is called on the iterator and
true is returned, false otherwise. If the iterator does
not support removing elements, an UnsupportedOperationException
is thrown.public E removeFirst()
LinkedList.removeFirst in interface Deque<E>NoSuchElementException - if this LinkedList is empty.public E removeLast()
LinkedList.removeLast in interface Deque<E>NoSuchElementException - if this LinkedList is empty.public Iterator<E> descendingIterator()
descendingIterator in interface Deque<E>Deque.descendingIterator()public boolean offerFirst(E e)
Deque.addFirst(E) method,
which can fail to insert an element only by throwing an exception.offerFirst in interface Deque<E>e - the element to addtrue if the element was added to this deque, else
falseDeque.offerFirst(java.lang.Object)public boolean offerLast(E e)
Deque.addLast(E) method,
which can fail to insert an element only by throwing an exception.offerLast in interface Deque<E>e - the element to addtrue if the element was added to this deque, else
falseDeque.offerLast(java.lang.Object)public E peekFirst()
null if this deque is empty.peekFirst in interface Deque<E>null if this deque is emptyDeque.peekFirst()public E peekLast()
null if this deque is empty.peekLast in interface Deque<E>null if this deque is emptyDeque.peekLast()public E pollFirst()
null if this deque is empty.pollFirst in interface Deque<E>null if this deque is emptyDeque.pollFirst()public E pollLast()
null if this deque is empty.pollLast in interface Deque<E>null if this deque is emptyDeque.pollLast()public E pop()
This method is equivalent to Deque.removeFirst().
pop in interface Deque<E>Deque.pop()public void push(E e)
IllegalStateException if no space is currently available.
This method is equivalent to Deque.addFirst(E).
push in interface Deque<E>e - the element to pushDeque.push(java.lang.Object)public boolean removeFirstOccurrence(Object o)
e such that
Objects.equals(o, e) (if such an element exists).
Returns true if this deque contained the specified element
(or equivalently, if this deque changed as a result of the call).removeFirstOccurrence in interface Deque<E>o - element to be removed from this deque, if presenttrue if an element was removed as a result of this callDeque.removeFirstOccurrence(java.lang.Object)public boolean removeLastOccurrence(Object o)
e such that
Objects.equals(o, e) (if such an element exists).
Returns true if this deque contained the specified element
(or equivalently, if this deque changed as a result of the call).removeLastOccurrence in interface Deque<E>o - element to be removed from this deque, if presenttrue if an element was removed as a result of this callDeque.removeLastOccurrence(java.lang.Object)public E set(int location, E object)
LinkedList
with the specified object.set in interface List<E>set in class AbstractSequentialList<E>location - the index at which to put the specified object.object - the object to add.ClassCastException - if the class of an object is inappropriate for this list.IllegalArgumentException - if an object cannot be added to this list.IndexOutOfBoundsException - if location < 0 || location >= size()public int size()
LinkedList.public boolean offer(E o)
Dequetrue upon success and false if no space is currently
available. When using a capacity-restricted deque, this method is
generally preferable to the Deque.add(E) method, which can fail to
insert an element only by throwing an exception.
This method is equivalent to Deque.offerLast(E).
public E poll()
Dequenull if this deque is empty.
This method is equivalent to Deque.pollFirst().
public E remove()
Dequepoll() only in that it
throws an exception if this deque is empty.
This method is equivalent to Deque.removeFirst().
public E peek()
Dequenull if this deque is empty.
This method is equivalent to Deque.peekFirst().
public E element()
Dequepeek only in that it throws an
exception if this deque is empty.
This method is equivalent to Deque.getFirst().
public Object[] toArray()
LinkedList.toArray in interface Collection<E>toArray in interface List<E>toArray in class AbstractCollection<E>LinkedList.Arrays.asList(Object[])public <T> T[] toArray(T[] contents)
LinkedList. If the specified array is large enough to hold the
elements, the specified array is used, otherwise an array of the same
type is created. If the specified array is used and is larger than this
LinkedList, the array element following the collection elements
is set to null.toArray in interface Collection<E>toArray in interface List<E>toArray in class AbstractCollection<E>T - the runtime type of the array to contain the collectioncontents - the array.LinkedList.ArrayStoreException - if the type of an element in this LinkedList cannot
be stored in the type of the specified array.