Java LinkedList class
The java.util.
LinkedList
class provides a doubly-linked list implementation of the interfaces named List and Deque.
It implements all optional list operations, and permits all elements (including null).
The operations that index into the list will traverse the list from the start or the end, whichever is closer to the specified index.
This class extends AbstractSequentialList and implements the List interface.
LinkedList Constructors
Following are the constructors supported by the LinkedList class.
Sl. No. | Constructor and Description |
1 | LinkedList ( ) This constructor builds an empty linked list. |
2 | LinkedList (Collection c) This constructor builds a linked list that is initialized with the elements of the collection c. |
LinkedList Methods
Apart from the methods inherited from its parent classes, LinkedList defines following methods −
Sl. No. | Method and Description |
1 | void add (int index, Object element) Inserts the specified element at the specified position index in this list. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index > size()). |
2 | boolean add (Object o) Appends the specified element to the end of this list. |
3 | boolean addAll (Collection c) 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. Throws NullPointerException if the specified collection is null. |
4 | boolean addAll (int index, Collection c) Inserts all of the elements in the specified collection into this list, starting at the specified position. Throws NullPointerException if the specified collection is null. |
5 | void addFirst (Object o) Inserts the given element at the beginning of this list. |
6 | void addLast (Object o) Appends the given element to the end of this list. |
7 | void clear() Removes all of the elements from this list. |
8 | Object clone() Returns a shallow copy of this LinkedList. |
9 | 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)). |
10 | Object get (int index) Returns the element at the specified position in this list. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()). |
11 | Object getFirst() Returns the first element in this list. Throws NoSuchElementException if this list is empty. |
12 | Object getLast() Returns the last element in this list. Throws NoSuchElementException if this list is empty. |
13 | 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. |
14 | 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. |
15 | ListIterator listIterator (int index) Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()). |
16 | Object remove (int index) Removes the element at the specified position in this list. Throws NoSuchElementException if this list is empty. |
17 | boolean remove (Object o) Removes the first occurrence of the specified element in this list. Throws NoSuchElementException if this list is empty. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()). |
18 | Object removeFirst() Removes and returns the first element from this list. Throws NoSuchElementException if this list is empty. |
19 | Object removeLast() Removes and returns the last element from this list. Throws NoSuchElementException if this list is empty. |
20 | Object set (int index, Object element) Replaces the element at the specified position in this list with the specified element. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()). |
21 | int size() Returns the number of elements in this list. |
22 | Object[ ] toArray() Returns an array containing all of the elements in this list in the correct order. Throws NullPointerException if the specified array is null. |
23 | Object[ ] toArray (Object[ ] a) Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array. |
ArrayList vs. LinkedList
ArrayList :-
- ArrayList is best choice if our frequent operation is retrieval operation.
- ArrayList is worst choice if our operation is insertion and deletion in the middle because internally several shift operations are performed.
- In ArrayList elements will be stored in consecutive memory locations hence retrieval operation will become easy.
LinkedList :
- LinkedList is best choice if our frequent operation is insertion and deletion in the middle.
- LinkedList is worst choice is our frequent operation is retrieval operation.
- In LinkedList the elements won’t be stored in consecutive memory location and hence retrieval operation will be complex.
Example
The following program illustrates different methods supported by the LinkedList class−
//LinkedListDemo.java
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
//Create a LinkedList collection of Integers
LinkedList<Integer> list = new LinkedList<Integer>();
int size;
Iterator<Integer> iterator;
//Add data in the list
list.add(11);
list.add(22);
list.add(33);
list.add(44);
size = list.size();
System.out.print("Linked list data: ");
//Create a iterator
iterator = list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
//Check list empty or not
if (list.isEmpty()) {
System.out.println("Linked list is empty");
}
else {
System.out.println("Linked list size: " + size);
}
//Add at the first place
list.addFirst(55);
System.out.println("Adding data at first location: 55");
System.out.print("Now the list contains: ");
iterator = list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Now the size of list: " + list.size());
//Adding last or append
list.addLast(66);
System.out.println("Adding data at last location: 66");
System.out.print("Now the list contain: ");
iterator = list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Now the size of list: " + list.size());
//Adding data at 3rd position
list.add(2, 99);
System.out.println("Adding data at 3rd location: 99");
System.out.print("Now the list contains: ");
iterator = list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Now the size of list: " + list.size());
//Retrieve first data
System.out.println("First data: " + list.getFirst());
//Retrieve last data
System.out.println("Last data: " + list.getLast());
//Retrieve specific data
System.out.println("Data at position 3 is: " + list.get(3));
//Remove first element
int first = list.removeFirst();
System.out.println("Data removed from 1st location: " + first);
System.out.print("Now the list contains: ");
iterator = list.iterator();
//After removing data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Now the size of list: " + list.size());
//Remove last element
int last = list.removeLast();
System.out.println("Data removed from last location: " + last);
System.out.print("Now the list contains: ");
iterator = list.iterator();
//After removing data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Now the size of list: " + list.size());
//Remove 2nd data
int second = list.remove(1);
System.out.println("Data removed from 2nd location: " + second);
System.out.print("Now the list contains: ");
iterator = list.iterator();
//After removing data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Now the size of list: " + list.size());
//Remove all elements
list.clear();
if (list.isEmpty()) {
System.out.println("Linked list is empty");
}
else {
System.out.println("Linked list size: " + size);
}
}
}
Output:
Linked list data: 11 22 33 44
Linked list size: 4
Adding data at first location: 55
Now the list contains: 55 11 22 33 44
Now the size of list: 5
Adding data at last location: 66
Now the list contain: 55 11 22 33 44 66
Now the size of list: 6
Adding data at 3rd location: 99
Now the list contains: 55 11 99 22 33 44 66
Now the size of list: 7
First data: 55
Last data: 66
Data at position 3 is: 22
Data removed from 1st location: 55
Now the list contains: 11 99 22 33 44 66
Now the size of list: 6
Data removed from last location: 66
Now the list contains: 11 99 22 33 44
Now the size of list: 5
Data removed from 2nd location: 99
Now the list contains: 11 22 33 44
Now the size of list: 4
Linked list is empty