Skip to content

Most visited

Recently visited

navigation
Added in API level 1

LinkedList

public class LinkedList
extends AbstractSequentialList<E> implements List<E>, Deque<E>, Queue<E>, Cloneable, Serializable

java.lang.Object
   ↳ java.util.AbstractCollection<E>
     ↳ java.util.AbstractList<E>
       ↳ java.util.AbstractSequentialList<E>
         ↳ java.util.LinkedList<E>


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.

Summary

Inherited fields

From class java.util.AbstractList

Public constructors

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.

Public methods

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, returning true upon success and 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.

<T> T[] toArray(T[] contents)

Returns an array containing all elements contained in this LinkedList.

Object[] toArray()

Returns a new array containing all elements contained in this LinkedList.

Inherited methods

From class java.util.AbstractSequentialList
From class java.util.AbstractList
From class java.util.AbstractCollection
From class java.lang.Object
From interface java.util.List
From interface java.util.Collection
From interface java.util.Deque
From interface java.util.Queue
From interface java.lang.Iterable

Public constructors

LinkedList

Added in API level 1
LinkedList ()

Constructs a new empty instance of LinkedList.

LinkedList

Added in API level 1
LinkedList (Collection<? extends E> collection)

Constructs a new instance of 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.

Parameters
collection Collection: the collection of elements to add.

Public methods

add

Added in API level 1
boolean add (E object)

Adds the specified object at the end of this LinkedList.

Parameters
object E: the object to add.
Returns
boolean always true

add

Added in API level 1
void add (int location, 
                E object)

Inserts the specified object into this 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.

Parameters
location int: the index at which to insert.
object E: the object to add.
Throws
IndexOutOfBoundsException if location < 0 || location > size()

addAll

Added in API level 1
boolean addAll (Collection<? extends E> collection)

Adds the objects in the specified Collection to this LinkedList.

Parameters
collection Collection: the collection of objects.
Returns
boolean true if this LinkedList is modified, false otherwise.

addAll

Added in API level 1
boolean addAll (int location, 
                Collection<? extends E> collection)

Inserts the objects in the specified collection at the specified location in this LinkedList. The objects are added in the order they are returned from the collection's iterator.

Parameters
location int: the index at which to insert.
collection Collection: the collection of objects
Returns
boolean true if this LinkedList is modified, false otherwise.
Throws
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()

addFirst

Added in API level 1
void addFirst (E object)

Adds the specified object at the beginning of this LinkedList.

Parameters
object E: the object to add.

addLast

Added in API level 1
void addLast (E object)

Adds the specified object at the end of this LinkedList.

Parameters
object E: the object to add.

clear

Added in API level 1
void clear ()

Removes all elements from this LinkedList, leaving it empty.

See also:

clone

Added in API level 1
Object clone ()

Returns a new LinkedList with the same elements and size as this LinkedList.

Returns
Object a shallow copy of this LinkedList.

See also:

contains

Added in API level 1
boolean contains (Object object)

Searches this LinkedList for the specified object.

Parameters
object Object: the object to search for.
Returns
boolean true if object is an element of this LinkedList, false otherwise

descendingIterator

Added in API level 9
Iterator<E> 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).

Returns
Iterator<E> an iterator over the elements in this deque in reverse sequence

See also:

element

Added in API level 1
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). This method differs from peek only in that it throws an exception if this deque is empty.

This method is equivalent to getFirst().

Returns
E the head of the queue represented by this deque

get

Added in API level 1
E get (int location)

Returns the element at the specified location in this list.

Parameters
location int: the index of the element to return.
Returns
E the element at the specified index.

getFirst

Added in API level 1
E getFirst ()

Returns the first element in this LinkedList.

Returns
E the first element.
Throws
NoSuchElementException if this LinkedList is empty.

getLast

Added in API level 1
E getLast ()

Returns the last element in this LinkedList.

Returns
E the last element
Throws
NoSuchElementException if this LinkedList is empty

indexOf

Added in API level 1
int indexOf (Object object)

Searches this list for the specified object and returns the index of the first occurrence.

Parameters
object Object: the object to search for.
Returns
int the index of the first occurrence of the object, or -1 if it was not found.

lastIndexOf

Added in API level 1
int lastIndexOf (Object object)

Searches this LinkedList for the specified object and returns the index of the last occurrence.

Parameters
object Object: the object to search for
Returns
int the index of the last occurrence of the object, or -1 if it was not found.

listIterator

Added in API level 1
ListIterator<E> listIterator (int location)

Returns a ListIterator on the elements of this LinkedList. The elements are iterated in the same order that they occur in the LinkedList. The iteration starts at the specified location.

Parameters
location int: the index at which to start the iteration
Returns
ListIterator<E> a ListIterator on the elements of this LinkedList
Throws
IndexOutOfBoundsException if location < 0 || location > size()

See also:

offer

Added in API level 1
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. When using a capacity-restricted deque, this method is generally preferable to the add(E) method, which can fail to insert an element only by throwing an exception.

This method is equivalent to offerLast(E).

Parameters
o E: the element to add
Returns
boolean true if the element was added to this deque, else false

offerFirst

Added in API level 9
boolean offerFirst (E e)

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 the addFirst(E) method, which can fail to insert an element only by throwing an exception.

Parameters
e E: the element to add
Returns
boolean true if the element was added to this deque, else false

See also:

offerLast

Added in API level 9
boolean offerLast (E e)

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 the addLast(E) method, which can fail to insert an element only by throwing an exception.

Parameters
e E: the element to add
Returns
boolean true if the element was added to this deque, else false

See also:

peek

Added in API level 1
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.

This method is equivalent to peekFirst().

Returns
E the head of the queue represented by this deque, or null if this deque is empty

peekFirst

Added in API level 9
E peekFirst ()

Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.

Returns
E the head of this deque, or null if this deque is empty

See also:

peekLast

Added in API level 9
E peekLast ()

Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.

Returns
E the tail of this deque, or null if this deque is empty

See also:

poll

Added in API level 1
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.

This method is equivalent to pollFirst().

Returns
E the first element of this deque, or null if this deque is empty

pollFirst

Added in API level 9
E pollFirst ()

Retrieves and removes the first element of this deque, or returns null if this deque is empty.

Returns
E the head of this deque, or null if this deque is empty

See also:

pollLast

Added in API level 9
E pollLast ()

Retrieves and removes the last element of this deque, or returns null if this deque is empty.

Returns
E the tail of this deque, or null if this deque is empty

See also:

pop

Added in API level 9
E 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 removeFirst().

Returns
E the element at the front of this deque (which is the top of the stack represented by this deque)

See also:

push

Added in API level 9
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, returning true upon success and throwing an IllegalStateException if no space is currently available.

This method is equivalent to addFirst(E).

Parameters
e E: the element to push

See also:

remove

Added in API level 1
E remove ()

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque). This method differs from poll only in that it throws an exception if this deque is empty.

This method is equivalent to removeFirst().

Returns
E the head of the queue represented by this deque

remove

Added in API level 1
E remove (int location)

Removes the object at the specified location from this LinkedList.

Parameters
location int: the index of the object to remove
Returns
E the removed object
Throws
IndexOutOfBoundsException if location < 0 || location >= size()

remove

Added in API level 1
boolean remove (Object object)

Removes one instance of the specified object from this Collection 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.

Parameters
object Object: the object to remove.
Returns
boolean true if this Collection is modified, false otherwise.

removeFirst

Added in API level 1
E removeFirst ()

Removes the first object from this LinkedList.

Returns
E the removed object.
Throws
NoSuchElementException if this LinkedList is empty.

removeFirstOccurrence

Added in API level 9
boolean removeFirstOccurrence (Object o)

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).

Parameters
o Object: element to be removed from this deque, if present
Returns
boolean true if an element was removed as a result of this call

See also:

removeLast

Added in API level 1
E removeLast ()

Removes the last object from this LinkedList.

Returns
E the removed object.
Throws
NoSuchElementException if this LinkedList is empty.

removeLastOccurrence

Added in API level 9
boolean removeLastOccurrence (Object o)

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).

Parameters
o Object: element to be removed from this deque, if present
Returns
boolean true if an element was removed as a result of this call

See also:

set

Added in API level 1
E set (int location, 
                E object)

Replaces the element at the specified location in this LinkedList with the specified object.

Parameters
location int: the index at which to put the specified object.
object E: the object to add.
Returns
E 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 if location < 0 || location >= size()

size

Added in API level 1
int size ()

Returns the number of elements in this LinkedList.

Returns
int the number of elements in this LinkedList.

toArray

Added in API level 1
T[] toArray (T[] contents)

Returns an array containing all elements contained in this 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.

Parameters
contents T: the array.
Returns
T[] an array of the elements from this LinkedList.
Throws
ArrayStoreException if the type of an element in this LinkedList cannot be stored in the type of the specified array.

toArray

Added in API level 1
Object[] toArray ()

Returns a new array containing all elements contained in this LinkedList.

Returns
Object[] an array of the elements from this LinkedList.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.