Package java.util
Class LinkedList<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.AbstractSequentialList<E>
java.util.LinkedList<E>
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<E>,Collection<E>,Deque<E>,List<E>,Queue<E>
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Queue<E>, Cloneable, Serializable
LinkedList is an implementation of
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.
- Since:
- 1.2
- See Also:
- Serialized Form
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
Constructors Constructor Description LinkedList()Constructs a new empty instance ofLinkedList.LinkedList(Collection<? extends E> collection)Constructs a new instance ofLinkedListthat holds all of the elements contained in the specifiedcollection. -
Method Summary
Modifier and Type Method Description voidadd(int location, E object)Inserts the specified object into thisLinkedListat the specified location.booleanadd(E object)Adds the specified object at the end of thisLinkedList.booleanaddAll(int location, Collection<? extends E> collection)Inserts the objects in the specified collection at the specified location in thisLinkedList.booleanaddAll(Collection<? extends E> collection)Adds the objects in the specified Collection to thisLinkedList.voidaddFirst(E object)Adds the specified object at the beginning of thisLinkedList.voidaddLast(E object)Adds the specified object at the end of thisLinkedList.voidclear()Removes all elements from thisLinkedList, leaving it empty.Objectclone()Returns a newLinkedListwith the same elements and size as thisLinkedList.booleancontains(Object object)Searches thisLinkedListfor the specified object.Iterator<E>descendingIterator()Returns an iterator over the elements in this deque in reverse sequential order.Eelement()Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque).Eget(int location)Returns the element at the specified location in this list.EgetFirst()Returns the first element in thisLinkedList.EgetLast()Returns the last element in thisLinkedList.intindexOf(Object object)Searches this list for the specified object and returns the index of the first occurrence.intlastIndexOf(Object object)Searches thisLinkedListfor the specified object and returns the index of the last occurrence.ListIterator<E>listIterator(int location)Returns a ListIterator on the elements of thisLinkedList.booleanoffer(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.booleanofferFirst(E e)Inserts the specified element at the front of this deque unless it would violate capacity restrictions.booleanofferLast(E e)Inserts the specified element at the end of this deque unless it would violate capacity restrictions.Epeek()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.EpeekFirst()Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.EpeekLast()Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.Epoll()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.EpollFirst()Retrieves and removes the first element of this deque, or returns null if this deque is empty.EpollLast()Retrieves and removes the last element of this deque, or returns null if this deque is empty.Epop()Pops an element from the stack represented by this deque.voidpush(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, returning true upon success and throwing an IllegalStateException if no space is currently available.Eremove()Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque).Eremove(int location)Removes the object at the specified location from thisLinkedList.booleanremove(Object object)Removes one instance of the specified object from thisCollectionif one is contained (optional).EremoveFirst()Removes the first object from thisLinkedList.booleanremoveFirstOccurrence(Object o)Removes the first occurrence of the specified element from this deque.EremoveLast()Removes the last object from thisLinkedList.booleanremoveLastOccurrence(Object o)Removes the last occurrence of the specified element from this deque.Eset(int location, E object)Replaces the element at the specified location in thisLinkedListwith the specified object.intsize()Returns the number of elements in thisLinkedList.Object[]toArray()Returns a new array containing all elements contained in thisLinkedList.<T> T[]toArray(T[] contents)Returns an array containing all elements contained in thisLinkedList.Methods inherited from class java.util.AbstractSequentialList
iteratorMethods inherited from class java.util.AbstractList
equals, hashCode, listIterator, removeRange, subListMethods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.List
containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, retainAll, subList
-
Constructor Details
-
LinkedList
public LinkedList()Constructs a new empty instance ofLinkedList. -
LinkedList
Constructs a new instance ofLinkedListthat holds all of the elements contained in the specifiedcollection. The order of the elements in this newLinkedListwill be determined by the iteration order ofcollection.- Parameters:
collection- the collection of elements to add.
-
-
Method Details
-
add
Inserts the specified object into thisLinkedListat the specified location. The object is inserted before any previous element at the specified location. If the location is equal to the size of thisLinkedList, the object is added at the end.- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractSequentialList<E>- Parameters:
location- the index at which to insert.object- the object to add.- Throws:
IndexOutOfBoundsException- iflocation < 0 || location > size()
-
add
Adds the specified object at the end of thisLinkedList. -
addAll
Inserts the objects in the specified collection at the specified location in thisLinkedList. The objects are added in the order they are returned from the collection's iterator.- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractSequentialList<E>- Parameters:
location- the index at which to insert.collection- the collection of objects- Returns:
trueif thisLinkedListis modified,falseotherwise.- Throws:
ClassCastException- if the class of an object is inappropriate for this list.IllegalArgumentException- if an object cannot be added to this list.IndexOutOfBoundsException- iflocation < 0 || location > size()
-
addAll
Adds the objects in the specified Collection to thisLinkedList.- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Parameters:
collection- the collection of objects.- Returns:
trueif thisLinkedListis modified,falseotherwise.
-
addFirst
Adds the specified object at the beginning of thisLinkedList. -
addLast
Adds the specified object at the end of thisLinkedList. -
clear
public void clear()Removes all elements from thisLinkedList, leaving it empty.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>- See Also:
List.isEmpty(),size
-
clone
Returns a newLinkedListwith the same elements and size as thisLinkedList. -
contains
Searches thisLinkedListfor the specified object.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceDeque<E>- Specified by:
containsin interfaceList<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
object- the object to search for.- Returns:
trueifobjectis an element of thisLinkedList,falseotherwise
-
get
Description copied from class:AbstractListReturns the element at the specified location in this list. -
getFirst
Returns the first element in thisLinkedList.- Specified by:
getFirstin interfaceDeque<E>- Returns:
- the first element.
- Throws:
NoSuchElementException- if thisLinkedListis empty.
-
getLast
Returns the last element in thisLinkedList.- Specified by:
getLastin interfaceDeque<E>- Returns:
- the last element
- Throws:
NoSuchElementException- if thisLinkedListis empty
-
indexOf
Description copied from class:AbstractListSearches this list for the specified object and returns the index of the first occurrence. -
lastIndexOf
Searches thisLinkedListfor the specified object and returns the index of the last occurrence.- Specified by:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classAbstractList<E>- Parameters:
object- the object to search for- Returns:
- the index of the last occurrence of the object, or -1 if it was not found.
-
listIterator
Returns a ListIterator on the elements of thisLinkedList. The elements are iterated in the same order that they occur in theLinkedList. The iteration starts at the specified location.- Specified by:
listIteratorin interfaceList<E>- Specified by:
listIteratorin classAbstractSequentialList<E>- Parameters:
location- the index at which to start the iteration- Returns:
- a ListIterator on the elements of this
LinkedList - Throws:
IndexOutOfBoundsException- iflocation < 0 || location > size()- See Also:
ListIterator
-
remove
Removes the object at the specified location from thisLinkedList.- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractSequentialList<E>- Parameters:
location- the index of the object to remove- Returns:
- the removed object
- Throws:
IndexOutOfBoundsException- iflocation < 0 || location >= size()
-
remove
Description copied from class:AbstractCollectionRemoves one instance of the specified object from thisCollectionif one is contained (optional). This implementation iterates over thisCollectionand tests for each elementereturned by the iterator, whethereis equal to the given object. Ifobject != nullthen this test is performed usingobject.equals(e), otherwise usingobject == null. If an element equal to the given object is found, then theremovemethod is called on the iterator andtrueis returned,falseotherwise. If the iterator does not support removing elements, anUnsupportedOperationExceptionis thrown. -
removeFirst
Removes the first object from thisLinkedList.- Specified by:
removeFirstin interfaceDeque<E>- Returns:
- the removed object.
- Throws:
NoSuchElementException- if thisLinkedListis empty.
-
removeLast
Removes the last object from thisLinkedList.- Specified by:
removeLastin interfaceDeque<E>- Returns:
- the removed object.
- Throws:
NoSuchElementException- if thisLinkedListis empty.
-
descendingIterator
Returns an iterator over the elements in this deque in reverse sequential order. The elements will be returned in order from last (tail) to first (head).- Specified by:
descendingIteratorin interfaceDeque<E>- Returns:
- an iterator over the elements in this deque in reverse sequence
- Since:
- 1.6
- See Also:
Deque.descendingIterator()
-
offerFirst
Inserts the specified element at the front of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to theDeque.addFirst(E)method, which can fail to insert an element only by throwing an exception.- Specified by:
offerFirstin interfaceDeque<E>- Parameters:
e- the element to add- Returns:
- true if the element was added to this deque, else false
- Since:
- 1.6
- See Also:
Deque.offerFirst(java.lang.Object)
-
offerLast
Inserts the specified element at the end of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to theDeque.addLast(E)method, which can fail to insert an element only by throwing an exception.- Specified by:
offerLastin interfaceDeque<E>- Parameters:
e- the element to add- Returns:
- true if the element was added to this deque, else false
- Since:
- 1.6
- See Also:
Deque.offerLast(java.lang.Object)
-
peekFirst
Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.- Specified by:
peekFirstin interfaceDeque<E>- Returns:
- the head of this deque, or null if this deque is empty
- Since:
- 1.6
- See Also:
Deque.peekFirst()
-
peekLast
Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.- Specified by:
peekLastin interfaceDeque<E>- Returns:
- the tail of this deque, or null if this deque is empty
- Since:
- 1.6
- See Also:
Deque.peekLast()
-
pollFirst
Retrieves and removes the first element of this deque, or returns null if this deque is empty.- Specified by:
pollFirstin interfaceDeque<E>- Returns:
- the head of this deque, or null if this deque is empty
- Since:
- 1.6
- See Also:
Deque.pollFirst()
-
pollLast
Retrieves and removes the last element of this deque, or returns null if this deque is empty.- Specified by:
pollLastin interfaceDeque<E>- Returns:
- the tail of this deque, or null if this deque is empty
- Since:
- 1.6
- See Also:
Deque.pollLast()
-
pop
Pops an element from the stack represented by this deque. In other words, removes and returns the first element of this deque.This method is equivalent to
Deque.removeFirst().- Specified by:
popin interfaceDeque<E>- Returns:
- the element at the front of this deque (which is the top of the stack represented by this deque)
- Since:
- 1.6
- See Also:
Deque.pop()
-
push
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, returning true upon success and throwing an IllegalStateException if no space is currently available.This method is equivalent to
Deque.addFirst(E).- Specified by:
pushin interfaceDeque<E>- Parameters:
e- the element to push- Since:
- 1.6
- See Also:
Deque.push(java.lang.Object)
-
removeFirstOccurrence
Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first element e such that (o==null ? e==null : o.equals(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).- Specified by:
removeFirstOccurrencein interfaceDeque<E>- Parameters:
o- element to be removed from this deque, if present- Returns:
- true if an element was removed as a result of this call
- Since:
- 1.6
- See Also:
Deque.removeFirstOccurrence(java.lang.Object)
-
removeLastOccurrence
Removes the last occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the last element e such that (o==null ? e==null : o.equals(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).- Specified by:
removeLastOccurrencein interfaceDeque<E>- Parameters:
o- element to be removed from this deque, if present- Returns:
- true if an element was removed as a result of this call
- Since:
- 1.6
- See Also:
Deque.removeLastOccurrence(java.lang.Object)
-
set
Replaces the element at the specified location in thisLinkedListwith the specified object.- Specified by:
setin interfaceList<E>- Overrides:
setin classAbstractSequentialList<E>- Parameters:
location- the index at which to put the specified object.object- the object to add.- Returns:
- the previous element at the index.
- Throws:
ClassCastException- if the class of an object is inappropriate for this list.IllegalArgumentException- if an object cannot be added to this list.IndexOutOfBoundsException- iflocation < 0 || location >= size()
-
size
public int size()Returns the number of elements in thisLinkedList. -
offer
Description copied from interface:DequeInserts 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. When using a capacity-restricted deque, this method is generally preferable to theDeque.add(E)method, which can fail to insert an element only by throwing an exception.This method is equivalent to
Deque.offerLast(E). -
poll
Description copied from interface:DequeRetrieves 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.This method is equivalent to
Deque.pollFirst(). -
remove
Description copied from interface:DequeRetrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque). This method differs frompollonly in that it throws an exception if this deque is empty.This method is equivalent to
Deque.removeFirst(). -
peek
Description copied from interface:DequeRetrieves, 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.This method is equivalent to
Deque.peekFirst(). -
element
Description copied from interface:DequeRetrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque). This method differs frompeekonly in that it throws an exception if this deque is empty.This method is equivalent to
Deque.getFirst(). -
toArray
Returns a new array containing all elements contained in thisLinkedList.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractCollection<E>- Returns:
- an array of the elements from this
LinkedList.
-
toArray
public <T> T[] toArray(T[] contents)Returns an array containing all elements contained in thisLinkedList. 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 thisLinkedList, the array element following the collection elements is set to null.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractCollection<E>- Parameters:
contents- the array.- Returns:
- an array of the elements from this
LinkedList. - Throws:
ArrayStoreException- if the type of an element in thisLinkedListcannot be stored in the type of the specified array.
-