Java LinkedList Class
The LinkedList class is a part of java collection framework. It is available inside the java.util package. The LinkedList class extends AbstractSequentialList class and implements List and Deque interface.
The elements of LinkedList are organized as the elements of linked list data structure.
The LinkedList class is used to create a dynamic list of elements that can grow or shrunk as needed.
🔔 The LinkedList is a child class of AbstractSequentialList
🔔 The LinkedList implements interfaces like List, Deque, Cloneable, and Serializable.
🔔 The LinkedList allows to store duplicate data values.
🔔 The LinkedList maintains the order of insertion.
LinkedList class declaration
The LinkedList class has the following declaration.
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, Serializable
LinkedList class constructors
The LinkedList class has the following constructors.
- LinkedList( ) - Creates an empty List.
- LinkedList(Collection c) - Creates a List with given collection of elements.
Operations on LinkedList
The LinkedList class allow us to perform several operations like adding, accesing, deleting, updating, looping, etc. Let's look at each operation with examples.
Adding Items
The LinkedList class has the following methods to add items.
- boolean add(E element) - Appends given element to the List.
- boolean addAll(Collection c) - Appends given collection of elements to the List.
- void add(int position, E element) - Inserts the given element at specified position.
- boolean addAll(int position, Collection c) - Inserts the given collection of elements at specified position.
- void addFirst(E element) - Inserts the given element at beggining of the list.
- void addLast(E element) - Inserts the given element at end of the list.
- boolean offer(E element) - Inserts the given element at end of the list.
- boolean offerFirst(E element) - Inserts the given element at beggining of the list.
- boolean offerLast(E element) - Inserts the given element at end of the list.
- void push(E element) - Inserts the given element at beggining of the list.
Let's consider an example program to illustrate adding items to the LinkedList.
import java.util.*;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> list_1 = new LinkedList<String>();
LinkedList list_2 = new LinkedList();
list_2.add(10);
list_2.add(20);
list_2.addFirst(5);
list_2.addLast(25);
list_2.offer(2);
list_2.offerFirst(1);
list_2.offerLast(10);
list_2.push(40);
list_1.addAll(list_2);
System.out.println("List_1: " + list_1);
System.out.println("List_2: " + list_2);
}
}
Accessing Items
The LinkedList class has the following methods to access items.
- E get(int position) - Returns element at specified position from the LinkedList.
- E element( ) - Returns the first element from the invoking LinkedList.
- E getFirst( ) - Returns the first element from the invoking LinkedList.
- E getLast( ) - Returns the last element from the invoking LinkedList.
- E peek( ) - Returns the first element from the invoking LinkedList.
- E peekFirst( ) - Returns the first element from the invoking LinkedList, and returns null if list is empty.
- E peekLast( ) - Returns the last element from the invoking LinkedList, and returns null if list is empty.
- int indexOf(E element) - Returns the index value of given element first occurence in the LinkedList.
- int lastIndexOf(E element) - Returns the index value of given element last occurence in the LinkedList.
- E pop( ) - Returns the first element from the invoking LinkedList.
Let's consider an example program to illustrate accessing items from the LinkedList.
import java.util.*;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList list_1 = new LinkedList();
for(int i = 1; i <= 10; i++)
list_1.add(i);
System.out.println("List is " + list_1 + "\n");
System.out.println("get(position) - " + list_1.get(3));
System.out.println("getFirst() - " + list_1.getFirst());
System.out.println("getLast() - " + list_1.getLast());
System.out.println("element() - " + list_1.element());
System.out.println("peek() - " + list_1.peek());
System.out.println("peekFirst() - " + list_1.peekFirst());
System.out.println("peekLast() - " + list_1.peekLast());
System.out.println("pop() - " + list_1.pop());
System.out.println("indexOf(element) - " + list_1.indexOf(5));
System.out.println("lastIndexOf(element) - " + list_1.lastIndexOf(5));
}
}
Updating Items
The LinkedList class has the following methods to update or change items.
- E set(int index, E newElement) - Replace the element at specified index with newElement in the invoking LinkedList.
Let's consider an example program to illustrate updating items in the LinkedList.
import java.util.*;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList list_1 = new LinkedList();
for(int i = 1; i <= 10; i++)
list_1.add(i);
System.out.println("List is " + list_1 + "\n");
list_1.set(3, 50);
System.out.println("List after update at index 3 is\n" + list_1 + "\n");
}
}
Removing Items
The LinkedList class has the following methods to remove items.
- E remove( ) - Removes the first element from the invoking LinkedList.
- E remove(int index) - Removes the element at specified index in the invoking LinkedList.
- boolean remove(Object element) - Removes the first occurrence of the given element from the invoking LinkedList.
- E removeFirst( ) - Removes the first element from the invoking LinkedList.
- E removeLast( ) - Removes the last element from the invoking LinkedList.
- boolean removeFirstOccurrence(Object element) - Removes from the first occurrence of the given element from the invoking LinkedList.
- boolean removeLastOccurrence(Object element) - Removes from the last occurrence of the given element from the invoking LinkedList.
- E poll( ) - Removes the first element from the LinkedList, and returns null if the list is empty.
- E pollFirst( ) - Removes the first element from the LinkedList, and returns null if the list is empty.
- E pollLast( ) - Removes the last element from the LinkedList, and returns null if the list is empty.
- E pop( ) - Removes the first element from the LinkedList.
- void clear( ) - Removes all the elements from the LinkedList.
Let's consider an example program to illustrate removing items from the LinkedList.
import java.util.*;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList list_1 = new LinkedList();
for(int i = 1; i <= 10; i++)
list_1.add(i);
System.out.println("List initially is " + list_1);
list_1.remove();
System.out.println("\nList after remove()\n" + list_1);
list_1.remove(3);
System.out.println("\nList after remove(index)\n" + list_1);
list_1.removeFirst();
System.out.println("\nList after removeFirst()\n" + list_1);
list_1.removeLast();
System.out.println("\nList after removeLast()\n" + list_1);
list_1.removeFirstOccurrence(4);
System.out.println("\nList after removeFirstOccurrence()\n" + list_1);
list_1.removeLastOccurrence(7);
System.out.println("\nList after removeLastOccurrence()\n" + list_1);
list_1.pop();
System.out.println("\nList after pop()\n" + list_1);
list_1.clear();
System.out.println("\nList after clear()\n" + list_1);
}
}
Other utility methods
The LinkedList class has the following methods to work with elements of it.
- int size( ) - Returns the total number of elements in the invoking LinkedList.
- boolean isEmpty( ) - Returns true if the list is empty otherwise returns false.
- boolean contains(Object element) - Returns true if the list contains given element otherwise returns false.
- void sort(Comparator c) - Sorts all the elements of invoking list based on the given comparator.
- List[ ] subList(int startIndex, int endIndex) - Returns list of elements starting from startIndex to endIndex-1.
- Object clone( ) - Returns a shallow copy of an LinkedList.
- Object[ ] toArray( ) - Returns an array of Object instances that contains all the elements from invoking LinkedList.
- Spliterator spliterator( ) - Creates spliterator over the elements in a list.
- void trimToSize( ) - Used to trim a LinkedList instance to the number of elements it contains.
Let's consider an example program to illustrate other utility methods of the LinkedList.
import java.util.*;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList list_1 = new LinkedList();
for(int i = 1; i <= 10; i++)
list_1.add(i);
System.out.println("List initially is " + list_1);
System.out.println("Size() - " + list_1.size());
System.out.println("isEmpty() - " + list_1.isEmpty());
System.out.println("contains(4) - " + list_1.contains(4));
System.out.println("subList(start, end-1) - " + list_1.subList(2, 6));
}
}
Consolidated list of methods
The following table providess a consolidated view of all methods of LinkedList.
Method | Description |
---|---|
boolean add(E element) | Appends given element to the List. |
boolean addAll(Collection c) | Appends given collection of elements to the List. |
void add(int index, E element) | Inserts the given element at specified index. |
boolean addAll(int index, Collection c) | Inserts the given collection of elements at specified index. |
void addFirst(E element) | Inserts the given element at beggining of the list. |
void addLast(E element) | Inserts the given element at end of the list. |
boolean offer(E element) | Inserts the given element at end of the list. |
boolean offerFirst(E element) | Inserts the given element at beggining of the list. |
boolean offerLast(E element) | Inserts the given element at end of the list. |
void push(E element) | Inserts the given element at beggining of the list. |
E get(int index) | Returns element at specified index from the list. |
E getFirst( ) | Returns the first element from the list. |
E getLast( ) | Returns the last element from the list. |
E element( ) | Returns the first element from the list. |
E peek( ) | Returns the first element from the list. |
E peekFirst( ) | Returns the first element from the invoking LinkedList, and returns null if list is empty. |
E peekLast( ) | Returns the last element from the invoking LinkedList, and returns null if list is empty. |
int indexOf(E element) | Returns the index value of given element first occurence in the list. |
int lastIndexOf(E element) | Returns the index value of given element last occurence in the list. |
E set(int index, E newElement) | Replace the element at specified index with newElement in the invoking list. |
E remove() | Removes the first element from the invoking list. |
E remove(int index) | Removes the element at specified index in the invoking list. |
boolean remove(Object element) | Removes the first occurence of the given element from the invoking list. |
boolean removeAll(Collection c) | Removes the given collection of elements from the invoking LinkedList. |
E removeFirst() | Removes the first element from the invoking list. |
E removeLast() | Removes the first element from the invoking list. |
E removeFirstOccurrence(Object element) | Removes from the first occurrence of the given element from the invoking LinkedList. |
E removeLastOccurrence(Object element) | Removes from the last occurrence of the given element from the invoking LinkedList. |
E poll( ) | Removes the first element from the LinkedList, and returns null if the list is empty. |
E pollFirst( ) | Removes the first element from the LinkedList, and returns null if the list is empty. |
E pollLast( ) | Removes the last element from the LinkedList, and returns null if the list is empty. |
E pop( ) | Removes the first element from the LinkedList. |
void clear( ) | Removes all the elements from the LinkedList. |
int size( ) | Returns the total number of elements in the invoking LinkedList. |
boolean isEmpty( ) | Returns true if the list is empty otherwise returns false. |
boolean contains(Object element) | Returns true if the list contains given element otherwise returns false. |
void sort(Comparator c) | Sorts all the elements of invoking list based on the given comparator. |
List[ ] subList(int startIndex, int endIndex) | Returns list of elements starting from startIndex to endIndex-1. |
Object clone( ) | Returns a shallow copy of an LinkedList. |
Object[ ] toArray( ) | Returns an array of Object instances that contains all the elements from invoking LinkedList. |
Spliterator spliterator( ) | Creates spliterator over the elements in a list. |
void trimToSize( ) | Used to trim a LinkedList instance to the number of elements it contains. |