Java LinkedList Class



Introduction

The Java LinkedList class operations perform we can expect 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.

Class declaration

Following is the declaration for java.util.LinkedList class −

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

Parameters

Following is the parameter for java.util.LinkedList class −

E − This is the type of elements held in this collection.

Field

Fields inherited from class java.util.AbstractList.

Class constructors

Sr.No. Constructor & Description
1

LinkedList()

This constructs constructs an empty list.

2

LinkedList(Collection<? extends E> c)

This constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Class methods

Sr.No. Method & Description
1 boolean add(E e)

This method appends the specified element to the end of this list.

2 boolean addAll(Collection<? extends E> c)

This method 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.

3 void addFirst(E e)

This method returns inserts the specified element at the beginning of this list.

4 void addLast(E e)

This method returns appends the specified element to the end of this list.

5 void clear()

This method removes all of the elements from this list.

6 Object clone()

This method returns returns a shallow copy of this LinkedList.

7 boolean contains(Object o)

This method returns true if this list contains the specified element.

8 Iterator<E> descendingIterator()

This method returns an iterator over the elements in this deque in reverse sequential order.

9 E element()

This method retrieves, but does not remove, the head (first element) of this list.

10 E get(int index)

This method returns the element at the specified position in this list.

11 E getFirst()

This method returns the first element in this list.

12 E getLast()

This method returns the last element in this list.

13 int indexOf(Object o)

This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

14 int lastIndexOf(Object o)

This method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

15 ListIterator<E> listIterator(int index)

This method returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.

16 boolean offer(E e)

This method adds the specified element as the tail (last element) of this list.

17 boolean offerFirst(E e)

This method inserts the specified element at the front of this list.

18 boolean offerLast(E e)

This method inserts the specified element at the end of this list.

19 E peek()

This method retrieves, but does not remove, the head (first element) of this list.

20 E peekFirst()

This method retrieves, but does not remove, the first element of this list, or returns null if this list is empty.

21 E peekLast()

This method retrieves, but does not remove, the last element of this list, or returns null if this list is empty.

22 E poll()

This method retrieves and removes the head (first element) of this list.

23 E pollFirst()

This method retrieves and removes the first element of this list, or returns null if this list is empty.

24 E pollLast()

This method retrieves and removes the last element of this list, or returns null if this list is empty.

25 E pop()

This method pops an element from the stack represented by this list.

26 void push(E e)

This method pushes an element onto the stack represented by this list.

27 E remove()

This method retrieves and removes the head (first element) of this list.

28 E removeFirst()

This method removes and returns the first element from this list.

29 boolean removeFirstOccurrence(Object o)

This method removes the first occurrence of the specified element in this list (when traversing the list from head to tail).

30 E removeLast()

This method removes and returns the last element from this list.

31 boolean removeLastOccurrence(Object o)

This method removes the last occurrence of the specified element in this list (when traversing the list from head to tail).

32 E set(int index, E element)

This method replaces the element at the specified position in this list with the specified element.

33 int size()

This method returns the number of elements in this list.

34 Spliterator<E> spliterator()

This method creates a late-binding and fail-fast Spliterator over the elements in this list.

35 Object[] toArray()

This method returns an array containing all of the elements in this list in proper sequence (from first to last element).

Methods inherited

This class inherits methods from the following classes −

  • java.util.AbstractSequentialList
  • java.util.AbstractList
  • java.util.AbstractCollection
  • java.util.Object
  • java.util.List
  • java.util.Deque

Adding element to a LinkedList Example

The following example shows the usage of Java LinkedList add(E) method to add Integers. We're adding couple of Integers to the LinkedList object using add() method calls per element and then print each element to show the elements added.

package com.tutorialspoint;

import java.util.LinkedList;

public class LinkedListDemo {
   public static void main(String[] args) {
      
      // create an empty linked list 
      LinkedList<Integer> linkedList = new LinkedList<>();

      // use add() method to add elements in the linkedList
      linkedList.add(20);
      linkedList.add(30);
      linkedList.add(20);
      linkedList.add(30);
      linkedList.add(15);
      linkedList.add(22);
      linkedList.add(11);

      // let us print all the elements available in linkedList
      for (Integer number : linkedList) {
         System.out.println("Number = " + number);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Number = 20
Number = 30
Number = 20
Number = 30
Number = 15
Number = 22
Number = 11
Advertisements