In Java LinkedList class is a generic class it provides a linked-list data structure for storing and managing elements. This class implements List, Queue, and DeQueue interfaces.
Java LinkedList :
Some of the key features of the LinkedList class include:
-
LinkedList class is an implementation of doubly-linked list data structure, this allows operations to traverse the list from the beginning to end and from end to beginning as required.
-
LinkedList has the capability to perform all List operations and store all types of elements including null and duplicate elements.
- LinkedList has the capability to maintain the insertion order.
- Modification of the data inside LinkedList is fast when compared to ArrayList.
- Capability to accept null elements.
- LinkedList class is not synchronized.
Constructors in Java LinkedList :
-
LinkedList():Creates an empty LinkedList instance.
-
LinkedList(Collection<? extends E>c):Creates a LinkedList instance and initializes it with the elements in the collection c.
Methods in Java LinkedList :
The Java LinkedList class enables you to use the add(), get(), set(), remove(), and toArray() methods as like ArrayList.
In addition to that, the LinkedList class provides implementations for the methods in the Queue and DeQueue interfaces. These methods include:
-
E peek():Returns the first element in the linked list.
-
E poll():Returns the first element in the linked list and removes it from the list.
-
void addFirst(E e) and boolean offerFirst(E e):Insert the specified element to the start of the linked list.
-
void addLast(E e) and boolean offerLast(E e):Insert the specified element at the end of the linked list.
-
E getFirst() and E peekFirst():Return the first element in the linked list.
-
E getLast() and E peekLast():Return the last element in the linked list.
Note: The implementations provided by the ArrayList and LinkedList classes are not synchronized.
Basic Operations on Java LinkedList
package com.onlinetutorials;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.add(10);
ll.add("200");
ll.add(50);
ll.add(null);
System.out.println("Elements in LinkedList : "+ll);
// Add Element to LinkedList at First Place
ll.offerFirst(20);
ll.addFirst(30);
// Get the First Element from LinkedList
System.out.println("First element : "+ ll.peek());
// It gets the first element and removes from the LinkedList
System.out.println("First element : "+ ll.poll());
// Insert the elements in LinkedList at Last
ll.addLast("Linked");
ll.offerLast("List");
// get First element from LinkedList
System.out.println(ll.getFirst());
System.out.println(ll.peekFirst());
System.out.println("ll : "+ll);
}
}
Output :
Elements in LinkedList : [10, 200, 50, null]
First element : 30
First element : 30
20
20
ll : [20, 10, 200, 50, null, Linked, List]
We can say that the above LinkedList can be represented in the memory like below.
If we add an element to LinkedList “ll”, each node will be created like above. And each node will point to the next node.
Example: 10 is pointing to 200,200 pointing to 50 and 50 is pointing to null.
Remove Elements from LinkedList :
If we want to delete an element from the LinkedList at the middle, the node will be removed from the LinkedList and the pointers will point to the nearest node.
For example, if we want to remove 50 elements from the LinkedList, we need to use remove() method on “ll” LinckedList instance like ll.remove(“50”);
On the above diagram representing the deletion of an element from LinkedList.
When do we use LinkedList?
If our repeated operation is to insert or delete in the middle of the list, we can go with LinkedList instead of ArrayList.
Happy Learning 🙂