All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.sun.java.util.collections.LinkedList

java.lang.Object
   |
   +----com.sun.java.util.collections.AbstractCollection
           |
           +----com.sun.java.util.collections.AbstractList
                   |
                   +----com.sun.java.util.collections.AbstractSequentialList
                           |
                           +----com.sun.java.util.collections.LinkedList

public class LinkedList
extends AbstractSequentialList
implements List, Cloneable, 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, LinkedList provides uniformly named methods to get, remove and insert an element at the beginning and end of the List. These operations allow LinkedList to be used as a stack, queue, or double-ended queue (deque).

All of the stack/queue/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 begining or the end, whichever is closer to the specified index.

Note that this implementation is not synchronized. If multiple threads access a LinkedList concurrently, and at least one of the threads modifies the LinkedList 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 LinkedList. If no such object exists, the LinkedList should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the LinkedList:

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

The Iterators returned by LinkedList's iterator and listIterator methods are fail-fast: if the LinkedList 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.

See Also:
List, ArrayList, Vector, synchronizedList

Constructor Index

 o LinkedList()
Constructs an empty Linked List.
 o LinkedList(Collection)
Constructs a LinkedList containing the elements of the specified Collection, in the order they are returned by the Collection's iterator.

Method Index

 o add(int, Object)
Inserts the specified element at the specified position in this List.
 o add(Object)
Appends the specified element to the end of this List.
 o addAll(Collection)
Appends all of the elements in the specified Collection to the end of this this LinkedList, in the order that they are returned by the specified Collection's Iterator.
 o addAll(int, Collection)
Inserts all of the elements in the specified Collection into this LinkedList, starting at the specified position.
 o addFirst(Object)
Inserts the given element at the beginning of this List.
 o addLast(Object)
Appends the given element to the end of this List.
 o clear()
Removes all of the elements from this List.
 o clone()
Returns a shallow copy of this LinkedList.
 o contains(Object)
Returns true if this List contains the specified element.
 o get(int)
Returns the element at the specified position in this List.
 o getFirst()
Returns the first Element in this List.
 o getLast()
Returns the last Element in this List.
 o indexOf(Object)
Returns the index in this List of the first occurrence of the specified element, or -1 if the List does not contain this element.
 o lastIndexOf(Object)
Returns the index in this List of the last occurrence of the specified element, or -1 if the List does not contain this element.
 o listIterator(int)
Returns a ListIterator of the elements in this List (in proper sequence), starting at the specified position in the list.
 o remove(int)
Removes the element at the specified position in this List.
 o remove(Object)
Removes the first occurrence of the specified element in this List.
 o removeFirst()
Removes and returns the first Element from this List.
 o removeLast()
Removes and returns the last Element from this List.
 o set(int, Object)
Replaces the element at the specified position in this List with the specified element.
 o size()
Returns the number of elements in this List.
 o toArray()
Returns an array containing all of the elements in this LinkedList in the correct order.
 o toArray(Object[])
Returns an array containing all of the elements in this LinkedList in the correct order.

Constructors

 o LinkedList
 public LinkedList()
Constructs an empty Linked List.

 o LinkedList
 public LinkedList(Collection c)
Constructs a LinkedList containing the elements of the specified Collection, in the order they are returned by the Collection's iterator.

Methods

 o getFirst
 public Object getFirst()
Returns the first Element in this List.

Returns:
the first Element in this List.
 o getLast
 public Object getLast()
Returns the last Element in this List.

Returns:
the last Element in this List.
Throws: NoSuchElementException
List is empty.
 o removeFirst
 public Object removeFirst()
Removes and returns the first Element from this List.

Returns:
the first Element from this List.
Throws: NoSuchElementException
List is empty.
 o removeLast
 public Object removeLast()
Removes and returns the last Element from this List.

Returns:
the last Element from this List.
Throws: NoSuchElementException
List is empty.
 o addFirst
 public void addFirst(Object o)
Inserts the given element at the beginning of this List.

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

 o contains
 public boolean contains(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)).

Parameters:
o - element whose presence in this List is to be tested.
Returns:
true if this List contains the specified element.
Overrides:
contains in class AbstractCollection
 o size
 public int size()
Returns the number of elements in this List.

Returns:
the number of elements in this List.
Overrides:
size in class AbstractCollection
 o add
 public boolean add(Object o)
Appends the specified element to the end of this List.

Parameters:
o - element to be appended to this List.
Returns:
true (as per the general contract of Collection.add).
Overrides:
add in class AbstractList
 o remove
 public boolean remove(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).

Parameters:
o - element to be removed from this List, if present.
Returns:
true if the List contained the specified element.
Overrides:
remove in class AbstractCollection
 o addAll
 public boolean addAll(Collection c)
Appends all of the elements in the specified Collection to the end of this this LinkedList, 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 the specified Collection is this LinkedList, and this LinkedList is nonempty.)

Parameters:
index - index at which to insert first element from the specified collection.
c - elements to be inserted into this LinkedList.
Throws: IndexOutOfBoundsException
index out of range (index < 0 || index > size()).
Overrides:
addAll in class AbstractCollection
 o addAll
 public boolean addAll(int index,
                       Collection c)
Inserts all of the elements in the specified Collection into this LinkedList, 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 LinkedList in the order that they are returned by the specified Collection's iterator.

Parameters:
index - index at which to insert first element from the specified collection.
c - elements to be inserted into this LinkedList.
Throws: IndexOutOfBoundsException
index out of range (index < 0 || index > size()).
Overrides:
addAll in class AbstractSequentialList
 o clear
 public void clear()
Removes all of the elements from this List.

Overrides:
clear in class AbstractCollection
 o get
 public Object get(int index)
Returns the element at the specified position in this List.

Parameters:
index - index of element to return.
Returns:
the element at the specified position in this List.
Throws: IndexOutOfBoundsException
index is out of range (index < 0 || index >= size()).
Overrides:
get in class AbstractSequentialList
 o set
 public Object set(int index,
                   Object element)
Replaces the element at the specified position in this List with the specified element.

Parameters:
index - index of element to replace.
element - element to be stored at the specified position.
Returns:
the element previously at the specified position.
Throws: IndexOutOfBoundsException
index out of range (index < 0 || index >= size()).
Overrides:
set in class AbstractSequentialList
 o add
 public void add(int index,
                 Object element)
Inserts the specified element at the specified position in this List. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted.
Throws: IndexOutOfBoundsException
index is out of range (index < 0 || index > size()).
Overrides:
add in class AbstractSequentialList
 o remove
 public Object remove(int index)
Removes the element at the specified position in this List. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the List.

Parameters:
index - the index of the element to removed.
Returns:
the element previously at the specified position.
Throws: IndexOutOfBoundsException
index out of range (index < 0 || index >= size()).
Overrides:
remove in class AbstractSequentialList
 o indexOf
 public int indexOf(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. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters:
o - element to search for.
Returns:
the index in this List of the first occurrence of the specified element, or -1 if the List does not contain this element.
Overrides:
indexOf in class AbstractList
 o lastIndexOf
 public int lastIndexOf(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. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters:
o - element to search for.
Returns:
the index in this List of the last occurrence of the specified element, or -1 if the List does not contain this element.
Overrides:
lastIndexOf in class AbstractList
 o listIterator
 public ListIterator listIterator(int index)
Returns a ListIterator of the elements in this List (in proper sequence), starting at the specified position in the list. Obeys the general contract of List.listIterator(int).

The ListIterator is fail-fast: if the LinkedList is structurally modified at any time after the Iterator is created, in any way except through the ListIterator'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.

Parameters:
index - index of first element to be returned from the ListIterator (by a call to getNext).
Returns:
a ListIterator of the elements in this List (in proper sequence), starting at the specified position in the list.
Throws: IndexOutOfBoundsException
index is out of range (index < 0 || index > size()).
Overrides:
listIterator in class AbstractSequentialList
See Also:
listIterator
 o clone
 public Object clone()
Returns a shallow copy of this LinkedList. (The elements themselves are not cloned.)

Returns:
a shallow copy of this LinkedList.
Overrides:
clone in class Object
 o toArray
 public Object[] toArray()
Returns an array containing all of the elements in this LinkedList in the correct order.

Returns:
an array containing all of the elements in this LinkedList in the correct order.
Overrides:
toArray in class AbstractCollection
 o toArray
 public Object[] toArray(Object a[])
Returns an array containing all of the elements in this LinkedList in the correct order. The runtime type of the returned array is that of the specified array. If the LinkedList fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this LinkedList.

If the LinkedList fits in the specified array with room to spare (i.e., the array has more elements than the LinkedList), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the LinkedList only if the caller knows that the LinkedList does not contain any null elements.

Parameters:
a - the array into which the elements of the LinkedList are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of the LinkedList.
Throws: ArrayStoreException
the runtime type of a is not a supertype of the runtime type of every element in this LinkedList.
Overrides:
toArray in class AbstractCollection

All Packages  Class Hierarchy  This Package  Previous  Next  Index