java.util
Class LinkedList<E>

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.util.AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
      extended by java.util.AbstractList sample code for java.util.AbstractList definition code for java.util.AbstractList <E>
          extended by java.util.AbstractSequentialList sample code for java.util.AbstractSequentialList definition code for java.util.AbstractSequentialList <E>
              extended by java.util.LinkedList<E>
Type Parameters:
E - the type of elements held in this collection
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable , Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable , Iterable sample code for java.lang.Iterable definition code for java.lang.Iterable <E>, Collection sample code for java.util.Collection definition code for java.util.Collection <E>, List sample code for java.util.List definition code for java.util.List <E>, Queue sample code for java.util.Queue definition code for java.util.Queue <E>

public class LinkedList<E>
extends AbstractSequentialList sample code for java.util.AbstractSequentialList definition code for java.util.AbstractSequentialList <E>
implements List sample code for java.util.List definition code for java.util.List <E>, Queue sample code for java.util.Queue definition code for java.util.Queue <E>, Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable , Serializable sample code for java.io.Serializable definition code for java.io.Serializable

Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (including null). In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue (deque).

The class implements the Queue interface, providing first-in-first-out queue operations for add, poll, etc. Other stack and deque operations could be easily recast in terms of the standard list operations. They're included here primarily for convenience, though they may run slightly faster than the equivalent List operations.

All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

Note that this implementation is not synchronized. If multiple threads access a list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

     List list = Collections.synchronizedList(new LinkedList(...));
 

The iterators returned by the this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

This class is a member of the Java Collections Framework.

Since:
1.2
See Also:
List sample code for java.util.List definition code for java.util.List , ArrayList sample code for java.util.ArrayList definition code for java.util.ArrayList , Vector sample code for java.util.Vector definition code for java.util.Vector , Collections.synchronizedList(List) sample code for java.util.Collections.synchronizedList(java.util.List) definition code for java.util.Collections.synchronizedList(java.util.List) , Serialized Form

Field Summary
 
Fields inherited from class java.util.AbstractList sample code for java.util.AbstractList definition code for java.util.AbstractList
modCount sample code for java.util.AbstractList.modCount definition code for java.util.AbstractList.modCount
 
Constructor Summary
LinkedList sample code for java.util.LinkedList.LinkedList() definition code for java.util.LinkedList.LinkedList() ()
          Constructs an empty list.
LinkedList sample code for java.util.LinkedList.LinkedList(java.util.Collection) definition code for java.util.LinkedList.LinkedList(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
          Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
 
Method Summary
 boolean add sample code for java.util.LinkedList.add(E) definition code for java.util.LinkedList.add(E) (E o)
          Appends the specified element to the end of this list.
 void add sample code for java.util.LinkedList.add(int, E) definition code for java.util.LinkedList.add(int, E) (int index, E element)
          Inserts the specified element at the specified position in this list.
 boolean addAll sample code for java.util.LinkedList.addAll(java.util.Collection) definition code for java.util.LinkedList.addAll(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
          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.
 boolean addAll sample code for java.util.LinkedList.addAll(int, java.util.Collection) definition code for java.util.LinkedList.addAll(int, java.util.Collection) (int index, Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
          Inserts all of the elements in the specified collection into this list, starting at the specified position.
 void addFirst sample code for java.util.LinkedList.addFirst(E) definition code for java.util.LinkedList.addFirst(E) (E o)
          Inserts the given element at the beginning of this list.
 void addLast sample code for java.util.LinkedList.addLast(E) definition code for java.util.LinkedList.addLast(E) (E o)
          Appends the given element to the end of this list.
 void clear sample code for java.util.LinkedList.clear() definition code for java.util.LinkedList.clear() ()
          Removes all of the elements from this list.
 Object sample code for java.lang.Object definition code for java.lang.Object clone sample code for java.util.LinkedList.clone() definition code for java.util.LinkedList.clone() ()
          Returns a shallow copy of this LinkedList.
 boolean contains sample code for java.util.LinkedList.contains(java.lang.Object) definition code for java.util.LinkedList.contains(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  o)
          Returns true if this list contains the specified element.
 E element sample code for java.util.LinkedList.element() definition code for java.util.LinkedList.element() ()
          Retrieves, but does not remove, the head (first element) of this list.
 E get sample code for java.util.LinkedList.get(int) definition code for java.util.LinkedList.get(int) (int index)
          Returns the element at the specified position in this list.
 E getFirst sample code for java.util.LinkedList.getFirst() definition code for java.util.LinkedList.getFirst() ()
          Returns the first element in this list.
 E getLast sample code for java.util.LinkedList.getLast() definition code for java.util.LinkedList.getLast() ()
          Returns the last element in this list.
 int indexOf sample code for java.util.LinkedList.indexOf(java.lang.Object) definition code for java.util.LinkedList.indexOf(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  o)
          Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
 int lastIndexOf sample code for java.util.LinkedList.lastIndexOf(java.lang.Object) definition code for java.util.LinkedList.lastIndexOf(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  o)
          Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
 ListIterator sample code for java.util.ListIterator definition code for java.util.ListIterator <E> listIterator sample code for java.util.LinkedList.listIterator(int) definition code for java.util.LinkedList.listIterator(int) (int index)
          Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.
 boolean offer sample code for java.util.LinkedList.offer(E) definition code for java.util.LinkedList.offer(E) (E o)
          Adds the specified element as the tail (last element) of this list.
 E peek sample code for java.util.LinkedList.peek() definition code for java.util.LinkedList.peek() ()
          Retrieves, but does not remove, the head (first element) of this list.
 E poll sample code for java.util.LinkedList.poll() definition code for java.util.LinkedList.poll() ()
          Retrieves and removes the head (first element) of this list.
 E remove sample code for java.util.LinkedList.remove() definition code for java.util.LinkedList.remove() ()
          Retrieves and removes the head (first element) of this list.
 E remove sample code for java.util.LinkedList.remove(int) definition code for java.util.LinkedList.remove(int) (int index)
          Removes the element at the specified position in this list.
 boolean remove sample code for java.util.LinkedList.remove(java.lang.Object) definition code for java.util.LinkedList.remove(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  o)
          Removes the first occurrence of the specified element in this list.
 E removeFirst sample code for java.util.LinkedList.removeFirst() definition code for java.util.LinkedList.removeFirst() ()
          Removes and returns the first element from this list.
 E removeLast sample code for java.util.LinkedList.removeLast() definition code for java.util.LinkedList.removeLast() ()
          Removes and returns the last element from this list.
 E set sample code for java.util.LinkedList.set(int, E) definition code for java.util.LinkedList.set(int, E) (int index, E element)
          Replaces the element at the specified position in this list with the specified element.
 int size sample code for java.util.LinkedList.size() definition code for java.util.LinkedList.size() ()
          Returns the number of elements in this list.
 Object sample code for java.lang.Object definition code for java.lang.Object [] toArray sample code for java.util.LinkedList.toArray() definition code for java.util.LinkedList.toArray() ()
          Returns an array containing all of the elements in this list in the correct order.
<T> T[]
toArray sample code for java.util.LinkedList.toArray(T[]) definition code for java.util.LinkedList.toArray(T[]) (T[] a)
          Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array.
 
Methods inherited from class java.util.AbstractSequentialList sample code for java.util.AbstractSequentialList definition code for java.util.AbstractSequentialList
iterator sample code for java.util.AbstractSequentialList.iterator() definition code for java.util.AbstractSequentialList.iterator()
 
Methods inherited from class java.util.AbstractList sample code for java.util.AbstractList definition code for java.util.AbstractList
equals sample code for java.util.AbstractList.equals(java.lang.Object) definition code for java.util.AbstractList.equals(java.lang.Object) , hashCode sample code for java.util.AbstractList.hashCode() definition code for java.util.AbstractList.hashCode() , listIterator sample code for java.util.AbstractList.listIterator() definition code for java.util.AbstractList.listIterator() , removeRange sample code for java.util.AbstractList.removeRange(int, int) definition code for java.util.AbstractList.removeRange(int, int) , subList sample code for java.util.AbstractList.subList(int, int) definition code for java.util.AbstractList.subList(int, int)
 
Methods inherited from class java.util.AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection
containsAll sample code for java.util.AbstractCollection.containsAll(java.util.Collection) definition code for java.util.AbstractCollection.containsAll(java.util.Collection) , isEmpty sample code for java.util.AbstractCollection.isEmpty() definition code for java.util.AbstractCollection.isEmpty() , removeAll sample code for java.util.AbstractCollection.removeAll(java.util.Collection) definition code for java.util.AbstractCollection.removeAll(java.util.Collection) , retainAll sample code for java.util.AbstractCollection.retainAll(java.util.Collection) definition code for java.util.AbstractCollection.retainAll(java.util.Collection) , toString sample code for java.util.AbstractCollection.toString() definition code for java.util.AbstractCollection.toString()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 
Methods inherited from interface java.util.List sample code for java.util.List definition code for java.util.List
containsAll sample code for java.util.List.containsAll(java.util.Collection) definition code for java.util.List.containsAll(java.util.Collection) , equals sample code for java.util.List.equals(java.lang.Object) definition code for java.util.List.equals(java.lang.Object) , hashCode sample code for java.util.List.hashCode() definition code for java.util.List.hashCode() , isEmpty sample code for java.util.List.isEmpty() definition code for java.util.List.isEmpty() , iterator sample code for java.util.List.iterator() definition code for java.util.List.iterator() , listIterator sample code for java.util.List.listIterator() definition code for java.util.List.listIterator() , removeAll sample code for java.util.List.removeAll(java.util.Collection) definition code for java.util.List.removeAll(java.util.Collection) , retainAll sample code for java.util.List.retainAll(java.util.Collection) definition code for java.util.List.retainAll(java.util.Collection) , subList sample code for java.util.List.subList(int, int) definition code for java.util.List.subList(int, int)
 

Constructor Detail

LinkedList sample code for java.util.LinkedList() definition code for java.util.LinkedList()

public LinkedList()
Constructs an empty list.


LinkedList sample code for java.util.LinkedList(java.util.Collection<? extends E>) definition code for java.util.LinkedList(java.util.Collection<? extends E>)

public LinkedList(Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Parameters:
c - the collection whose elements are to be placed into this list.
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the specified collection is null.
Method Detail

getFirst sample code for java.util.LinkedList.getFirst() definition code for java.util.LinkedList.getFirst()

public E getFirst()
Returns the first element in this list.

Returns:
the first element in this list.
Throws:
NoSuchElementException sample code for java.util.NoSuchElementException definition code for java.util.NoSuchElementException - if this list is empty.

getLast sample code for java.util.LinkedList.getLast() definition code for java.util.LinkedList.getLast()

public E getLast()
Returns the last element in this list.

Returns:
the last element in this list.
Throws:
NoSuchElementException sample code for java.util.NoSuchElementException definition code for java.util.NoSuchElementException - if this list is empty.

removeFirst sample code for java.util.LinkedList.removeFirst() definition code for java.util.LinkedList.removeFirst()

public E removeFirst()
Removes and returns the first element from this list.

Returns:
the first element from this list.
Throws:
NoSuchElementException sample code for java.util.NoSuchElementException definition code for java.util.NoSuchElementException - if this list is empty.

removeLast sample code for java.util.LinkedList.removeLast() definition code for java.util.LinkedList.removeLast()

public E removeLast()
Removes and returns the last element from this list.

Returns:
the last element from this list.
Throws:
NoSuchElementException sample code for java.util.NoSuchElementException definition code for java.util.NoSuchElementException - if this list is empty.

addFirst sample code for java.util.LinkedList.addFirst(E) definition code for java.util.LinkedList.addFirst(E)

public void addFirst(E o)
Inserts the given element at the beginning of this list.

Parameters:
o - the element to be inserted at the beginning of this list.

addLast sample code for java.util.LinkedList.addLast(E) definition code for java.util.LinkedList.addLast(E)

public void addLast(E o)
Appends the given element to the end of this list. (Identical in function to the add method; included only for consistency.)

Parameters:
o - the element to be inserted at the end of this list.

contains sample code for java.util.LinkedList.contains(java.lang.Object) definition code for java.util.LinkedList.contains(java.lang.Object)

public boolean contains(Object sample code for java.lang.Object definition code for java.lang.Object  o)
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

Specified by:
contains sample code for java.util.Collection.contains(java.lang.Object) definition code for java.util.Collection.contains(java.lang.Object) in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
contains sample code for java.util.List.contains(java.lang.Object) definition code for java.util.List.contains(java.lang.Object) in interface List sample code for java.util.List definition code for java.util.List <E>
Overrides:
contains sample code for java.util.AbstractCollection.contains(java.lang.Object) definition code for java.util.AbstractCollection.contains(java.lang.Object) in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Parameters:
o - element whose presence in this list is to be tested.
Returns:
true if this list contains the specified element.

size sample code for java.util.LinkedList.size() definition code for java.util.LinkedList.size()

public int size()
Returns the number of elements in this list.

Specified by:
size sample code for java.util.Collection.size() definition code for java.util.Collection.size() in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
size sample code for java.util.List.size() definition code for java.util.List.size() in interface List sample code for java.util.List definition code for java.util.List <E>
Specified by:
size sample code for java.util.AbstractCollection.size() definition code for java.util.AbstractCollection.size() in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Returns:
the number of elements in this list.

add sample code for java.util.LinkedList.add(E) definition code for java.util.LinkedList.add(E)

public boolean add(E o)
Appends the specified element to the end of this list.

Specified by:
add sample code for java.util.Collection.add(E) definition code for java.util.Collection.add(E) in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
add sample code for java.util.List.add(E) definition code for java.util.List.add(E) in interface List sample code for java.util.List definition code for java.util.List <E>
Overrides:
add sample code for java.util.AbstractList.add(E) definition code for java.util.AbstractList.add(E) in class AbstractList sample code for java.util.AbstractList definition code for java.util.AbstractList <E>
Parameters:
o - element to be appended to this list.
Returns:
true (as per the general contract of Collection.add).

remove sample code for java.util.LinkedList.remove(java.lang.Object) definition code for java.util.LinkedList.remove(java.lang.Object)

public boolean remove(Object sample code for java.lang.Object definition code for java.lang.Object  o)
Removes the first occurrence of the specified element in this list. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).

Specified by:
remove sample code for java.util.Collection.remove(java.lang.Object) definition code for java.util.Collection.remove(java.lang.Object) in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
remove sample code for java.util.List.remove(java.lang.Object) definition code for java.util.List.remove(java.lang.Object) in interface List sample code for java.util.List definition code for java.util.List <E>
Overrides:
remove sample code for java.util.AbstractCollection.remove(java.lang.Object) definition code for java.util.AbstractCollection.remove(java.lang.Object) in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Parameters:
o - element to be removed from this list, if present.
Returns:
true if the list contained the specified element.

addAll sample code for java.util.LinkedList.addAll(java.util.Collection<? extends E>) definition code for java.util.LinkedList.addAll(java.util.Collection<? extends E>)

public boolean addAll(Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
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. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this list, and this list is nonempty.)

Specified by:
addAll sample code for java.util.Collection.addAll(java.util.Collection) definition code for java.util.Collection.addAll(java.util.Collection) in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
addAll sample code for java.util.List.addAll(java.util.Collection) definition code for java.util.List.addAll(java.util.Collection) in interface List sample code for java.util.List definition code for java.util.List <E>
Overrides:
addAll sample code for java.util.AbstractCollection.addAll(java.util.Collection) definition code for java.util.AbstractCollection.addAll(java.util.Collection) in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Parameters:
c - the elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the specified collection is null.
See Also:
AbstractCollection.add(Object) sample code for java.util.AbstractCollection.add(E) definition code for java.util.AbstractCollection.add(E)

addAll sample code for java.util.LinkedList.addAll(int, java.util.Collection<? extends E>) definition code for java.util.LinkedList.addAll(int, java.util.Collection<? extends E>)

public boolean addAll(int index,
                      Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified collection's iterator.

Specified by:
addAll sample code for java.util.List.addAll(int, java.util.Collection) definition code for java.util.List.addAll(int, java.util.Collection) in interface List sample code for java.util.List definition code for java.util.List <E>
Overrides:
addAll sample code for java.util.AbstractSequentialList.addAll(int, java.util.Collection) definition code for java.util.AbstractSequentialList.addAll(int, java.util.Collection) in class AbstractSequentialList sample code for java.util.AbstractSequentialList definition code for java.util.AbstractSequentialList <E>
Parameters:
index - index at which to insert first element from the specified collection.
c - elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
Throws:
IndexOutOfBoundsException sample code for java.lang.IndexOutOfBoundsException definition code for java.lang.IndexOutOfBoundsException - if the specified index is out of range (index < 0 || index > size()).
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the specified collection is null.

clear sample code for java.util.LinkedList.clear() definition code for java.util.LinkedList.clear()

public void clear()
Removes all of the elements from this list.

Specified by:
clear sample code for java.util.Collection.clear() definition code for java.util.Collection.clear() in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
clear sample code for java.util.List.clear() definition code for java.util.List.clear() in interface List sample code for java.util.List definition code for java.util.List <E>
Overrides:
clear sample code for java.util.AbstractList.clear() definition code for java.util.AbstractList.clear() in class AbstractList