English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

LinkedList in Java

The LinkedList class extends AbstractSequentialList and implements the List interface. It provides a linked list data structure.

The following are the constructors supported by the LinkedList class.

NumberConstructor and Description
1LinkedList()
This constructor builds an empty linked list.
2LinkedList(Collection c)
This constructor builds a linked list that uses the collectioncto initialize the elements.

In addition to the methods inherited from its superclass, LinkedList also defines the following methods.

NumberMethod and Description
1void add(int index, Object element)
Inserts the specified element at the specified index in this list. If the specified index is out of range (index < 0 || index size()),then an IndexOutOfBoundsException is thrown.
2boolean add(Object o)
Appends the specified element to the end of this list.
3boolean addAll(Collection c)
Appends all elements from the specified collection to the end of this list in the order returned by the iterator of the specified collection. If the specified collection is null, it throws a NullPointerException.
4boolean addAll(int index, Collection c)
Starting from the specified position, inserts all elements from the specified collection into this list. If the specified collection is null, it throws a NullPointerException.
5void addFirst(Object o)
Inserts the specified element at the beginning of this list.
6void addLast(Object o)
Appends the specified element to the end of this list.
7voidclear()
Removes all elements from this list.
8Objectclone()
Returns a shallow copy of this LinkedList.
9boolean contains(Object o)
If this list contains the specified element, it returns true. More formally, it returns true if and only if this list contains at least one element (e == null ? e == null : o.equals(e)).
10Object get(int index)
Returns the element at the specified position in this list. If the specified index is out of range (index < 0 || index >= size() ), size()),then an IndexOutOfBoundsException is thrown.
11ObjectgetFirst()
Returns the first element of this list. If this list is empty, then a NoSuchElementException is thrown.
12ObjectgetLast()
Returns the last element of this list. If this list is empty, then a NoSuchElementException is thrown.
13int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list; if the list does not contain the element, then returns-1.
14int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list; if the list does not contain the element, then returns-1.
15ListIterator listIterator(int index)
Starting from the specified position in the list (in the appropriate order,)Returns a list iterator over the elements of this list (starting at the specified index). If the specified index is out of range (index < 0 || index >= size() ), size()),then an IndexOutOfBoundsException is thrown.
16Object remove(int index)
Remove the element at the specified position in this list. If this list is empty, then a NoSuchElementException is thrown.
17boolean remove(Object o)
Remove the first occurrence of the specified element from this list. If this list is empty, then a NoSuchElementException is thrown. If the specified index is out of range (index < 0 || index >= size() ), size()),then an IndexOutOfBoundsException is thrown.
18YearObjectremoveFirst()
Remove and return the first element of this list. If this list is empty, then a NoSuchElementException is thrown.
19ObjectremoveLast()
Remove and return the last element of this list. If this list is empty, then a NoSuchElementException is thrown.
20Object set(int index, Object element)
Replace the element at the specified position in this list with the specified element. If the specified index is out of range (index < 0 || index >= size() ), size()),then an IndexOutOfBoundsException is thrown.
21intsize()
Returns the number of elements in this list.
22Object[]toArray()
Returns an array containing all of the elements of this list in the correct order. If the specified array is null, then a NullPointerException is thrown.
23Object[] toArray(Object[] a)
Returns an array containing all elements of this list in the correct order. The runtime type of the returned array is the runtime type of the specified array.

Example

The following program demonstrates several methods supported by LinkedList.

import java.util.*;
public class LinkedListDemo {
   public static void main(String args[]) {
      //Create a linked list
      LinkedList ll = new LinkedList();
      //Add elements to the linked list
      ll.add("F");
      ll.add("B");
      ll.add("D");
      ll.add("E");
      ll.add("C");
      ll.addLast("Z");
      ll.addFirst("A");
      ll.add(1, "A2
      System.out.println("Original contents of ll: ", + ll);
      //Remove an element from the linked list
      ll.remove("F");
      ll.remove(2);
      System.out.println("Contents of ll after deletion: ", + ll);
      //Delete the first and last elements
      ll.removeFirst();
      ll.removeLast();
      System.out.println("ll after deleting first and last: ", + ll);
      //Get and set a value
      Object val = ll.get(2);
      ll.set(2, (String) val + "Changed");
      System.out.println("ll after change: ", + ll);
   }
}

Output Result

Original contents of ll: [A, A2, F, B, D, E, C, Z]
Contents of ll after deletion: [A, A2, D, E, C, Z]
ll after deleting first and last: [A2, D, E, C]
ll after change: [A2, D, E Changed, C]